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

🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › null
JSON Schema - null
When a schema specifies a type of null, it has only one acceptable value: null. It's important to remember that in JSON, null isn't equivalent to something being absent. See Required Properties for an example.
Discussions

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
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 20, 2024
Help with "Trying to access array offset on value of type null"
Trying to access array offset on value of type null Imagine you have this code: $i_think_this_is_array = null; echo $i_think_this_is_array[0]; That's where the error happen. In your case, it's very likely that some of your queries don't return any data because there's no record matching some specific ID. But the problem is that your logic is very off, it's a convoluted way to achieve what you need. How could you fix it? A single SQL query, joining and fetching all the data your need in one go, then loop over the results to print them: // I'll try to format in a way to be easy to understand. // Defining an order and adding a limit are also important. $sql = 'SELECT nombre, artista, texto, valoracion, img, likes, dislikes, nombreusuario, generomusica FROM recomendacion_m INNER JOIN usuarios ON usuarios.idusuario = recomendacion_m.idusuario INNER JOIN genero_m ON genero_m.idgeneromusica = recomendacion_m.idgeneromusica ORDER BY nombre LIMIT 20'; $mysqliresult = $db->query($sql); // Return an array of associative arrays, representing rows and columns. // This is the standard way of dealing with many database records. $results = $mysqliresult->fetch_all(MYSQLI_ASSOC); // Little tip #1: the if/elseif/else block can be changed to a map (used later): $valoraciones = [ 1 => "../../common/img/1star.png", 2 => "../../common/img/2star.png", 3 => "../../common/img/3star.png", 4 => "../../common/img/4star.png", 5 => "../../common/img/5star.png", ]; // Little tip #2: avoid writing HTML content as/in PHP strings, it makes it very hard to write and understand. // The basic approach is to fetch all needed data in variables, "drop off" of PHP mode and start writing HTML using the alternative PHP syntax for templates (link below): ?> // From here, everything is "output". // some basic HTML code, then loop over the results: // simplified HTML for demonstration

Cancion:

Artista:

Genero:

Usuario:

... Alternative template syntax docs Foreach example with extra vars All data printed to HTML need to be escaped with htmlspecialchars(). Do you think this is very confusing or hard? I'd recommend getting a copy of PHP & MySQL book by Jon Duckett to learn more. More on reddit.com
🌐 r/PHPhelp
6
1
May 7, 2024
ConvertFrom-Json issue
I'd probably load up Fiddler , point it at your PowerShell process, and look at the raw HTTP response from the API when it fails for you (optionally copy it here as well). There might be something funky like double encoding happening where the response is being parsed, but the resulting object is just a single string value that's another embedded JSON string or something like that. More on reddit.com
🌐 r/PowerShell
18
4
June 19, 2019
🌐
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.
🌐
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.
🌐
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.
🌐
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

Find elsewhere
🌐
IBM
ibm.com › docs › en › baw › 24.0.x
Handling JSON null and empty arrays and objects
January 20, 2025 - Handling null and empty arrays and objects used in JSON data is described.
🌐
W3Schools
w3schools.com › js › js_json_datatypes.asp
W3Schools.com
JSON supports the Boolean values true and false. ... The value null represents an empty value.
🌐
Make Community
community.make.com › questions
Syntax for returning null / int in JSON - Questions - Make Community
February 20, 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…
🌐
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.
🌐
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...
🌐
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
🌐
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.
🌐
Sibasi Ltd
blog.sibasi.com › handling null values in parse json schema for power automate
Handling Null Values in Parse JSON Schema for Power Automate | Sibasi Ltd Blog
June 19, 2024 - Some of these fields might be null in your data. Here's an example of such data: [ { "Country": null, "UserNickname": "user123", "UserName": "John Doe", "address": "123 Main St\nAnytown\nCountry\n", "City": null } ]
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-check-if-json-key-value-is-null-in-javascript
How to Check if JSON Key Value is Null in JavaScript ? | GeeksforGeeks
April 16, 2024 - const jsonData = { key1: null, key2: "value2", key3: undefined, }; let res = false; for (let key in jsonData) { if (jsonData[key] === null) { res = true; break; } } if (res) { console.log( "Null value is present in the JSON object."); } else ...
🌐
Boomi
community.boomi.com › s › article › Working-with-Nulls-in-JSON
Article: Working with Nulls in JSON - Boomi Community
March 10, 2025 - If the business logic requires a null to be mapped to a null and an empty string to be mapped to an empty string, then this solution can be used in conjunction with Condition 2 (Empty String to Empty String). Figure 1: Required Option Set within the JSON profile to force a null element · Boomi will read a null value and an empty string as the same.
🌐
Snowflake Documentation
docs.snowflake.com › en › sql-reference › functions › is_null_value
IS_NULL_VALUE | Snowflake Documentation
Returns FALSE for a non-null JSON value. Returns NULL for a SQL NULL value. This example uses the IS_NULL_VALUE function.
🌐
R-project
svn.r-project.org › R-packages › trunk › RJSONIO › inst › doc › missingValues.html
JSON, null and NA
As such, it is useful for R to be able to import and export data in this format. Unfortunately, JSON is a little too simple and cannot faithfully represent all of the types and values in R. Most specifically, there is no way to support NA, Inf or NaN. Typically, these values are represented in JSON as "null".