Skip to main content

How to Write null in JSON

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

To say that a pair has no value in JSON, write null as its value: lowercase and without quotes, as in "phone": null. It isn't the same as an empty string, false, the text "null" or a pair that's left out, and this guide shows each difference with jq.

Syntax​

General form
null
  • null: the value for "no value", written in lowercase letters with no quotes around it.

The JSON standard, RFC 8259, counts null as a type of its own: JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). It's written as one of three literal names, with true and false, and The literal names MUST be lowercase. No other literal names are allowed.

The Example​

A file called contact.json describes a contact with two pairs: a name, Ada, and a phone with no value, which is null. jq, a command-line JSON tool, checks that the file is valid and shows the type of the value.

Prerequisites​

  • jq, to check the file. Run jq --version to see which one you have; this guide uses jq 1.8.2 (Download jq).

Step 1: Write the null Value​

Create a file called contact.json with this text:

macOS-style code window showing contact.json, a JSON object with name set to Ada and phone set to null, written in lowercase and without quotes.

contact.json
{
"name": "Ada",
"phone": null
}

The phone pair is in the file, but its value is null.

Step 2: Check It with jq​

In a terminal, in the folder with the file, run:

Terminal
jq . contact.json

jq's manual explains the .: the . program's main use is to validate and pretty-print the inputs. The file was valid, and jq printed null back as it is, without quotes:

Output
{
"name": "Ada",
"phone": null
}

Step 3: Check the Value's Type​

Run:

Terminal
jq '.phone | type' contact.json
Output
"null"

macOS-style terminal window where jq '.phone | type' contact.json prints the word null in double quotes, the name of the phone value's type.

.phone reads the value of phone, and | hands it on to type. jq's manual: The type function returns the type of its argument as a string, which is one of null, boolean, number, string, array or object. So the value's type is null, a type of its own. The quotes in the output are there because type answers with the type's name as text.

null Is Not "", false or "null"​

look-alikes.json puts null next to three values that are easy to mix up with it:

look-alikes.json
{
"null_value": null,
"empty_string": "",
"false_value": false,
"null_in_quotes": "null"
}
Terminal
jq 'map_values(type)' look-alikes.json
Output
{
"null_value": "null",
"empty_string": "string",
"false_value": "boolean",
"null_in_quotes": "string"
}

map_values(type) swapped each value for the name of its type and kept the names of the pairs; the manual says map_values outputs an array if given an array, or an object if given an object. Only the first value is null. "" is a string with no characters, false is a boolean, a true/false value, and "null" in quotes is text: the four letters n, u, l, l.

null Is Not a Left-Out Pair​

no-phone.json leaves the phone pair out:

no-phone.json
{
"name": "Ada"
}

Reading phone from both files gives the same answer:

Terminal
jq '.phone' contact.json no-phone.json
Output
null
null

jq read both files. Its manual says the values it reads are passed through the provided filter one at a time, so it printed one result per file. The manual calls a name a key, and says that .foo, here .phone, produces the value at the key "foo" if the key is present, or null otherwise. That's why no-phone.json gave null too. has tells the two apart:

Terminal
jq 'has("phone")' contact.json no-phone.json
Output
true
false

The manual: The builtin function has returns whether the input object has the given key, or the input array has an element at the given index. The first answer, true, is for contact.json, which has a phone pair whose value is null. The second, false, is for no-phone.json, which has no phone pair at all.

Common Mistakes​

I wrote Null with a capital letter in a copy of contact.json and ran jq . on it. jq 1.8.2 said:

Output
jq: parse error: Invalid numeric literal at line 4, column 0

jq didn't recognise Null and tried to read it as a number, which is why the message mentions one. It points at line 4, column 0, just after the line with Null, because jq checks a word once it reaches the end of it.

MistakeWhat happensDo this instead
A capital letter, Null or NULLInvalid numeric literalWrite null
A word from another programming language: None, nil or undefinedInvalid numeric literal for each of themWrite null
Quotes, "null"Valid, but it's text, not nullLeave the quotes out
Nothing after the colon, "phone":Unmatched '}' at the closing bracketWrite null, or leave the pair out

Troubleshooting​

SymptomLikely causeFix
Invalid numeric literal where null should beA capital letter, or a word such as NoneWrite null in lowercase
Invalid literalA misspelling such as nulWrite null
jq prints null for a pair that isn't in the file.phone gives null for a missing nameCheck with has("phone")
The value prints with quotes in jqIt's the text "null"Remove the quotes

FAQ​

Where can null go?​

Anywhere a value can: as the value of a pair, inside an array, or as a whole file. jq printed [1, null, 3] back as [1,null,3] with -c, its compact output, and a file that holds only null back as null. A whole file can be one value on its own, because, in the standard's words, A JSON text is a serialized value.

Can a name be null?​

No. The standard says A name is a string. A file with { null: "Ada" } laid out over three lines stopped jq with Object keys must be strings.

Should I write null or leave the pair out?​

The standard doesn't say; it depends on what the software that reads the file expects. jq read both as null, and only has told them apart.

Conclusion​

A pair with no value gets null: lowercase, without quotes. jq showed that its type is null, while "" and "null" are strings and false is a boolean. A left-out pair also read as null in jq, and only has("phone") told the two apart. Null, NULL, None, nil and undefined all stopped jq with Invalid numeric literal.

Additional Resources​