Let's evaluate the parsing of each:

http://jsfiddle.net/brandonscript/Y2dGv/

var json1 = '{}';
var json2 = '{"myCount": null}';
var json3 = '{"myCount": 0}';
var json4 = '{"myString": ""}';
var json5 = '{"myString": "null"}';
var json6 = '{"myArray": []}';

console.log(JSON.parse(json1)); // {}
console.log(JSON.parse(json2)); // {myCount: null}
console.log(JSON.parse(json3)); // {myCount: 0}
console.log(JSON.parse(json4)); // {myString: ""}
console.log(JSON.parse(json5)); // {myString: "null"}
console.log(JSON.parse(json6)); // {myArray: []}

The tl;dr here:

The fragment in the json2 variable is the way the JSON spec indicates null should be represented. But as always, it depends on what you're doing -- sometimes the "right" way to do it doesn't always work for your situation. Use your judgement and make an informed decision.


JSON1 {}

This returns an empty object. There is no data there, and it's only going to tell you that whatever key you're looking for (be it myCount or something else) is of type undefined.


JSON2 {"myCount": null}

In this case, myCount is actually defined, albeit its value is null. This is not the same as both "not undefined and not null", and if you were testing for one condition or the other, this might succeed whereas JSON1 would fail.

This is the definitive way to represent null per the JSON spec.


JSON3 {"myCount": 0}

In this case, myCount is 0. That's not the same as null, and it's not the same as false. If your conditional statement evaluates myCount > 0, then this might be worthwhile to have. Moreover, if you're running calculations based on the value here, 0 could be useful. If you're trying to test for null however, this is actually not going to work at all.


JSON4 {"myString": ""}

In this case, you're getting an empty string. Again, as with JSON2, it's defined, but it's empty. You could test for if (obj.myString == "") but you could not test for null or undefined.


JSON5 {"myString": "null"}

This is probably going to get you in trouble, because you're setting the string value to null; in this case, obj.myString == "null" however it is not == null.


JSON6 {"myArray": []}

This will tell you that your array myArray exists, but it's empty. This is useful if you're trying to perform a count or evaluation on myArray. For instance, say you wanted to evaluate the number of photos a user posted - you could do myArray.length and it would return 0: defined, but no photos posted.

Answer from brandonscript on Stack Overflow
Top answer
1 of 8
589

Let's evaluate the parsing of each:

http://jsfiddle.net/brandonscript/Y2dGv/

var json1 = '{}';
var json2 = '{"myCount": null}';
var json3 = '{"myCount": 0}';
var json4 = '{"myString": ""}';
var json5 = '{"myString": "null"}';
var json6 = '{"myArray": []}';

console.log(JSON.parse(json1)); // {}
console.log(JSON.parse(json2)); // {myCount: null}
console.log(JSON.parse(json3)); // {myCount: 0}
console.log(JSON.parse(json4)); // {myString: ""}
console.log(JSON.parse(json5)); // {myString: "null"}
console.log(JSON.parse(json6)); // {myArray: []}

The tl;dr here:

The fragment in the json2 variable is the way the JSON spec indicates null should be represented. But as always, it depends on what you're doing -- sometimes the "right" way to do it doesn't always work for your situation. Use your judgement and make an informed decision.


JSON1 {}

This returns an empty object. There is no data there, and it's only going to tell you that whatever key you're looking for (be it myCount or something else) is of type undefined.


JSON2 {"myCount": null}

In this case, myCount is actually defined, albeit its value is null. This is not the same as both "not undefined and not null", and if you were testing for one condition or the other, this might succeed whereas JSON1 would fail.

This is the definitive way to represent null per the JSON spec.


JSON3 {"myCount": 0}

In this case, myCount is 0. That's not the same as null, and it's not the same as false. If your conditional statement evaluates myCount > 0, then this might be worthwhile to have. Moreover, if you're running calculations based on the value here, 0 could be useful. If you're trying to test for null however, this is actually not going to work at all.


JSON4 {"myString": ""}

In this case, you're getting an empty string. Again, as with JSON2, it's defined, but it's empty. You could test for if (obj.myString == "") but you could not test for null or undefined.


JSON5 {"myString": "null"}

This is probably going to get you in trouble, because you're setting the string value to null; in this case, obj.myString == "null" however it is not == null.


JSON6 {"myArray": []}

This will tell you that your array myArray exists, but it's empty. This is useful if you're trying to perform a count or evaluation on myArray. For instance, say you wanted to evaluate the number of photos a user posted - you could do myArray.length and it would return 0: defined, but no photos posted.

2 of 8
282

null is not zero. It is not a value, per se: it is a value outside the domain of the variable indicating missing or unknown data.

There is only one way to represent null in JSON. Per the specs (RFC 4627 and json.org):

2.1.  Values

A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:

  false null true

🌐
IBM
ibm.com › docs › en › baw › 19.0.0
Handling JSON null and empty arrays and objects
August 11, 2022 - Handling null and empty arrays and objects used in JSON data is described.
Discussions

java - How do you deal with NULL values while creating JsonObject? - Software Engineering Stack Exchange
So you either use .add with a proper value what will get translated to null when you build the JSON, or you don't have the .add call. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 30, 2016
Best Practices to send for Empty Values of a JSON API?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
16
16
April 22, 2024
JSON serializer should exclude null values in JsonElement
Wherever you serialize you can apply serializer options with a ignore null condition. Like this: JsonSerializerOptions options = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; string myComponentJson = JsonSerializer.Serialize(myCompnent, options); The reason your attribute is not working is because it would ignore the whole JsonElement (Attributes or Internals) if it is null. But this does not apply to the inner JSON structure of the properties. More on reddit.com
🌐 r/dotnet
21
0
April 21, 2024
Removing default values while serializing using Newtonsoft.Json
I believe this is due to the fact that you're using a JObject rather than an actual class. As per this StackOverflow thread , a JObject with a null property actually stores a non-null JValue value with it's Type set to JTokenType.Null. There will be a similar issue for the default values. There's a related discussion on the GitHub repo : A JObject isn't serialized. It is written as is. Also worth noting that an empty array doesn't count as a "default" value; the default for an array would be null. More on reddit.com
🌐 r/dotnet
11
2
July 7, 2023
🌐
Reddit
reddit.com › r/learnprogramming › best practices to send for empty values of a json api?
r/learnprogramming on Reddit: Best Practices to send for Empty Values of a JSON API?
April 22, 2024 -

What do you choose? Getting lots of conflicting answers.

  1. Null

  2. Undefined (Omitted from JSON)

  3. Empty of that type?

    1. String = ""

    2. Number = 0

    3. Boolean = false

    4. Array = []

    5. Object = {}

CMS JSON API to be consumed by JS Framework Frontend

🌐
Thinking Matters
social-biz.org › 2023 › 12 › 21 › null-values-in-json
NULL Values in JSON | Thinking Matters
December 21, 2023 - Declaring that you will always treat a null value the same as an omitted value will simplify the logic handling them, and more importantly will clarify the meaning to the users so that they make fewer mistakes.
🌐
Newtonsoft
newtonsoft.com › json › help › html › T_Newtonsoft_Json_NullValueHandling.htm
NullValueHandling Enumeration
Movie movie = new Movie(); movie.Name = "Bad Boys III"; movie.Description = "It's no Bad Boys"; string included = JsonConvert.SerializeObject(movie, Formatting.Indented, new JsonSerializerSettings { }); // { // "Name": "Bad Boys III", // "Description": "It's no Bad Boys", // "Classification": null, // "Studio": null, // "ReleaseDate": null, // "ReleaseCountries": null // } string ignored = JsonConvert.SerializeObject(movie, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); // { // "Name": "Bad Boys III", // "Description": "It's no Bad Boys" // }
🌐
Baeldung
baeldung.com › home › json › jackson › include null value in json serialization
Include null Value in JSON Serialization | Baeldung
June 21, 2025 - This configuration ensures that null values are incorporated during serialization, ensuring precise representation in the resulting JSON output, even if the address field is null. In conclusion, it’s essential to handle null values appropriately when working with Java objects and converting them into JSON format.
🌐
Progress
docs.progress.com › corticon deployment › request and response examples › json and native json request and response messages › how to pass null values in a json request
How to pass null values in a JSON request
Passing a null value to any Corticon Server using JSON payloads is accomplished by either: Omitting the JSON attribute inside the JSON object Including the attribute name in the JSON Object with a value of JSONObject.NULL JSON payloads with null
Find elsewhere
🌐
Newtonsoft
newtonsoft.com › json › help › html › NullValueHandlingIgnore.htm
NullValueHandling setting
Person person = new Person { Name = "Nigal Newborn", Age = 1 }; string jsonIncludeNullValues = JsonConvert.SerializeObject(person, Formatting.Indented); Console.WriteLine(jsonIncludeNullValues); // { // "Name": "Nigal Newborn", // "Age": 1, // "Partner": null, // "Salary": null // } string jsonIgnoreNullValues = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(jsonIgnoreNullValues); // { // "Name": "Nigal Newborn", // "Age": 1 // }
🌐
Webdevtutor
webdevtutor.net › blog › c-sharp-json-null-value-handling
Handling Null Values in JSON with C#
In some cases, you may want to ignore properties with null values entirely during deserialization. This can be achieved by configuring the JSON serializer settings accordingly. Here's an example using Newtonsoft.Json: var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var result = JsonConvert.DeserializeObject<MyClass>(json, settings); Handling null values in JSON data is crucial for ensuring the reliability and integrity of your C# applications.
🌐
Howik
howik.com › home › data handling & json › handling null values in json: what you need to know
Handling Null Values in JSON: What You Need to Know - Howik
June 1, 2025 - According to the specs (RFC 4627 and json.org), a JSON value must be an object, array, number, or string, or one of the following three literal names: false, null, true. ... In this example, the "phone" field is set to null, indicating that ...
🌐
Devgex
devgex.com › en › article › 00001897
Representing Null Values in JSON: Standards and Best Practices - DevGex
October 28, 2025 - For example, when a server-side Integer object myCount has no value, it should generate {“myCount”: null} rather than omitting the property or using a zero value. This approach ensures structural integrity and clarity, enabling receivers to accurately distinguish between "property does not exist" and "property exists but has null value" scenarios. Handling null values for string types requires particular attention.
🌐
Toxigon
toxigon.com › home › programming › handling null values in json: practical tips and tricks
Handling Null Values in JSON: Practical Tips and Tricks - Toxigon
December 15, 2024 - But what about nested JSON data? That's where things can get a bit more complicated. One way to handle nested null values is to use recursive functions. A recursive function is a function that calls itself in order to solve a problem.
🌐
json-everything
blog.json-everything.net › posts › null-has-value-too
Null Has Value, Too | json-everything
June 20, 2024 - Similarly, you get null when querying the object. It all still works. One of the features of the JsonNode API is that you can find out where in the JSON structure a particular value exists by calling its .GetPath() method. This method returns a JSON Path (BTW, wrong construct) that starts from the root JSON value and leads to the value you have.
🌐
IBM
ibm.com › docs › en › cobol-zos › 6.4.0
Handling JSON null values
This section describes the ways to parse JSON null values using the JSON PARSE statement.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include null Value in JSON Serialization - Java Code Geeks
May 27, 2024 - The Jackson library from FasterXML is the most popular library for serializing Java objects to JSON and vice-versa. By default, it includes the null values, however, the default behavior can be overwritten with the @JsonInclude annotation and ObjectMapper.setSerializationInclusion method.
🌐
Albertmoreno
albertmoreno.dev › posts › json-api-design-to-allow-null-values-or-not-an-in-depth-look-at-the-pros-and-cons
JSON API Design: To Allow Null Values or Not? An In-depth Look at the Pros and Cons :: Hi! 👋 I'm Albert
February 12, 2023 - When it comes to managing null values and avoiding empty fields in JSON APIs, there are several best practices to keep in mind: Document your API: It is important to clearly document your API and the rules and conventions that it follows, including how null values and empty fields are handled.
🌐
Quora
quora.com › How-do-you-pass-a-null-value-in-JSON
How to pass a null value in JSON - Quora
Answer: In addition to strings, numbers, arrays, and objects, JSON supports three special values: true, false, and null. This page is really everything you need to know about JSON syntax. (Effective usage is another story…) https://www.json.org/json-en.html It is possible your intended question...
🌐
Jsontech
jsontech.net › home › learn › json null & boolean values
JSON Null & Boolean Values Explained | JSONTech.net
March 20, 2025 - Confusing null with undefined: undefined is not a valid JSON value. If you serialize a JavaScript object with undefined properties, they are silently dropped. Inconsistent null handling: Decide whether missing fields in your API responses should return null or be omitted entirely, then follow that convention everywhere.