Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Why JSON.stringify Silently Drops undefined Values, Functions, and Symbols From an Object

- 03.10.26 - ErcanOPAK

⚡ The Object Property That Disappeared on the Way to JSON

JSON has no representation for undefined, functions, or symbols – the format simply doesn’t define a way to encode them – so JSON.stringify doesn’t throw or warn when it encounters one, it just leaves the corresponding key out of the output entirely. A property that’s clearly present on the object, readable with dot notation and visible in a console.log, can vanish completely the moment that same object goes through JSON.stringify, with the resulting string looking shorter and the receiving end seeing a payload with fewer fields than the sender believed it sent.

🔎 The Problem

const payload = {
    orderId: 42,
    total: 99.99,
    discountCode: undefined,      // will be dropped entirely
    calculateTax: () => 0.08,     // will be dropped entirely
    [Symbol('internal')]: 'x',    // will be dropped entirely
};

console.log(payload);
// { orderId: 42, total: 99.99, discountCode: undefined, calculateTax: f, Symbol(internal): 'x' }
// Every property is visibly right there.

const json = JSON.stringify(payload);
console.log(json);
// '{"orderId":42,"total":99.99}'
// discountCode, calculateTax, and the symbol key are all just gone -
// not null, not an error, simply absent from the string.

✅ Fix: Normalize Values Before Serializing, and Check the Serialized Output Directly

  • Replace undefined with null (or a real default value) before serializing anything that needs to preserve the key – null does have a JSON representation and will survive the round trip.
  • Never assume an object’s in-memory shape matches its serialized shape – inspect the actual output of JSON.stringify(obj) when debugging a ‘missing field’ issue on the receiving end, instead of only checking the object before it was serialized.
  • If a function’s result needs to travel with the payload, call it and store its return value in a plain property beforehand – JSON.stringify can’t serialize behavior, only data, no matter how the object is structured.

⚠️ Why This Is Easy to Miss

  • console.log(payload) and JSON.stringify(payload) can show two visibly different sets of properties for the exact same object, and it’s easy to debug by eye on the first one without ever directly inspecting the second.
  • The silence is total – no warning in the console, no error thrown, no indication at serialization time that anything was left out – so the first sign of trouble is usually a confused ‘why is this field missing?’ on a completely different service that only ever sees the JSON.

JSON.stringify doesn’t refuse what it can’t represent – it just leaves it out, as if you’d never written it at all.

— JavaScript Tip

Related posts:

JavaScript: Use ?? Instead of || for Default Values

JavaScript: Cleaner API Consumption with Object Destructuring

JavaScript: Building a Data Dashboard with Charts

Post Views: 3

Post navigation

The AI Prompt That Strengthens a Weak Performance Review With Specific, Defensible Examples
Photoshop: Fix Guides That Go Missing Every Time a File Is Shared by a Teammate

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (948)
  • How to add default value for Entity Framework migrations for DateTime and Bool (938)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (624)
  • Add Constraint to SQL Table to ensure email contains @ (590)
  • Average of all values in a column that are not zero in SQL (553)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (526)
  • Find numbers with more than two decimal places in SQL (468)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (948)
  • How to add default value for Entity Framework migrations for DateTime and Bool (938)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com