Skip to main content
0 reactions

How to Write null in JSON

Card titled null in JSON, labelled JSON basics, with four ticked lines: “phone”: null, lowercase with no quotes, NULL gives a parse error, and “null” in quotes is a string.

In JSON, null says that a value is empty on purpose: the key is there, but it holds nothing. You write it as the word null in lowercase, with no quotes around it.

Every command and output on this page is real, run with jq 1.8.2 on macOS.

Syntax

SyntaxJSON
"key": null
  • "key": the name, in double quotes like every JSON key.
  • null: the empty value, in lowercase and without quotes. It can stand wherever a JSON value can, in an object or in an array.

See section 3 of RFC 8259 (opens in a new tab), the JSON standard: the three literal names false, null and true must be lowercase.

Prerequisites

The Example

A contact with a name and no phone number yet, saved in a file named contact.json.

Step 1: Write the JSON

contact.jsonJSON
{"name": "Asha", "phone": null}

Step 2: Check It with jq

jq . reads the file and prints it back neatly. If the JSON were broken, jq would stop with an error instead:

TerminalBash
jq . contact.json
OutputJSON
{
  "name": "Asha",
  "phone": null
}

Step 3: Read the Value

TerminalBash
jq '.phone' contact.json
OutputPlain text
null

.phone picks the value of the key phone: the key is there, and its value is null.

Common Mistakes

MistakeWhat happensDo this instead
Writing it in capitals: "phone": NULLjq stops: jq: parse error: Invalid numeric literal at line 1, column 31Write null in lowercase
Putting it in quotes: "phone": "null"It’s a string of four letters: jq '.phone' prints "null" with quotesLeave out the quotes

FAQ

Is null the same as an empty string?

No. An empty string, "", is text with nothing in it. null isn’t text at all, which is why "null" in quotes counts as a string.

Where will I see null?

Often in answers from web APIs, for a value that has nothing in it yet. For example, n8n’s API ends a list of workflows with "nextCursor":null when there is no next page.

Conclusion

Write null in lowercase and without quotes wherever a value is empty on purpose. NULL breaks the JSON, and "null" in quotes is just text.

Additional Resources

0 comments