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
281

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

Discussions

Syntax for returning null / int in JSON
Apologies if this has been answered, I’ve searched and been unable to find a solution. Can anyone help me with the correct syntax for returning a specific value if the value data they receive is empty? Here is my output: my connector for Make an API Sharepoint call… More on community.make.com
🌐 community.make.com
10
0
February 21, 2024
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 Schema
json-schema.org › understanding-json-schema › reference › null
JSON Schema - null
It's important to remember that in JSON, null isn't equivalent to something being absent. See Required Properties for an example.
🌐
Thinking Matters
social-biz.org › 2023 › 12 › 21 › null-values-in-json
NULL Values in JSON | Thinking Matters
December 21, 2023 - However if you use a Map in Java to receive the JSON, then it is possible to make a map member with the value of null, and that is different from not having the map member. You can iterate the members of a map, and get a null value. In my Java code I have to be careful to check the value and ignore it if it is null, so that null and omitted values are treated the same.
🌐
Make Community
community.make.com › questions
Syntax for returning null / int in JSON - Questions - Make Community
February 21, 2024 - Apologies if this has been answered, I’ve searched and been unable to find a solution. Can anyone help me with the correct syntax for returning a specific value if the value data they receive is empty? Here is my outpu…
🌐
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
🌐
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.
🌐
Calhoun
calhoun.io › how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided
How to determine if a JSON key has been set to null or not provided - Calhoun.io
When the key is not provided, and the value is implicitly null. Unfortunately (or fortunately?), that isn’t how Go, or really any typed languages work. We don’t declare a struct (or class) and magically lose a field when it isn’t defined. That field will always be present, and the value of that field will be nil or a valid value. For example, let’s imagine we have a Blog struct and the PublishedAt is an optional JSON attribute, your code might look like this.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include null Value in JSON Serialization - Java Code Geeks
May 27, 2024 - Table 1, Json vs Jackson on Null Value Serialization · These were two examples of maven projects which utilize both Jackson and Gson libraries for JSON null values serialization.
🌐
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.
🌐
IBM
ibm.com › docs › en › cobol-zos › 6.4.0
Generating JSON null values
This section describes the ways to generate JSON null values using the JSON GENERATE statement.
🌐
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

🌐
InterSystems
community.intersystems.com › post › send-null-string-field-json
Send null in a string field (JSON) | InterSystems Developer Community
COS language don't have null type, so null is converted to "". It is possible set a parameter, see: ... If %JSONNULL is true (=1), then unspecified properties are exported as the null value.
🌐
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...
🌐
Google Groups
groups.google.com › g › json-schema › c › J6QPptNBJgI
json schema for null strings, integers, enums, objects, arrays and empty arrays
September 12, 2013 - If you want to specify that a string field has no value, then you specify NULL. I see this as basically a knock-on effect from the world of C, where strings are pointers, but they can point to the special value NULL == 0. In JSON, however, properties are allowed to simply not exist.
🌐
Turbo360
turbo360.com › home › blog › logic app best practices, tips, and tricks: #31 specifying json schema elements nullable
Specifying JSON Schema elements null in Logic Apps
June 14, 2023 - So, once again, by default, when we generate our JSON Schemas inside Logic Apps, it does not allow any of these fields in the schema to be nullable. If you try to parse a record set with a null value, an error will be thrown.
🌐
Baeldung
baeldung.com › home › json › jackson › include null value in json serialization
Include null Value in JSON Serialization | Baeldung
June 21, 2025 - Here, we employ the GsonBuilder to set up a Gson instance using the serializeNulls() method. 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.
🌐
MarkLogic
docs.marklogic.com › json:null
json:null — MarkLogic 12 Product Documentation
Returns the JSON null value, which is an empty sequence to XQuery, but not a Sequence when passed to JavaScript.