Is null valid JSON (4 bytes, nothing else)
Asked Answered
D

2

67

Hot debate today:

Is the text stream null valid JSON?

According to http://www.ietf.org/rfc/rfc4627.txt?number=4627:

...
2. JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

Should this interpreted as both statements must be true in order for the text to be valid JSON?

However, many other libraries seem to permit it, and indeed, it seems like a single valid token could be a legitimate result.

Is there a definitive answer?

Deafen answered 15/12, 2011 at 21:39 Comment(0)
C
30

[Note! This answer is no longer correct, see answer by @niksnut.]

Yes, both statements must be true. The literal token null is a JSON value. A JSON text is a serialized object or array, which requires {} or [].

Cribwork answered 15/12, 2011 at 21:44 Comment(6)
How does it come then that most JSON parsers out there accept `null' as valid document? A typical examples are Node.JS (JSON.parse('null') => null or echo 'null' | python -mjson.tool => null.Daltondaltonism
echo '7' | python -mjson.tool yields 7, so I infer that tool apparently is willing to parse isolated JSON literal values as well as complete (object or array) JSON texts.Cribwork
Ruby’s JSON parser rejects just null as a value by default: ruby -r json -e 'JSON.parse("null")' givens “unexpected token at 'null' (JSON::ParserError)”Mollescent
Some parsers also choke on "[]".Seafarer
Note! This answer is no longer correct, see @Hickie answer belowColloquium
@moderators: (if any listening) Should we as community edit this answer with warning that it's outdated?Glimpse
H
96

RFC 7159 and its successor RFC 8259 drop the limitation that a JSON text must be an object or an array. The grammar specifies:

JSON-text = ws value ws

where

value = false / null / true / object / array / number / string

Thus, "null" is now a valid JSON document.

Hickie answered 24/8, 2016 at 13:40 Comment(0)
C
30

[Note! This answer is no longer correct, see answer by @niksnut.]

Yes, both statements must be true. The literal token null is a JSON value. A JSON text is a serialized object or array, which requires {} or [].

Cribwork answered 15/12, 2011 at 21:44 Comment(6)
How does it come then that most JSON parsers out there accept `null' as valid document? A typical examples are Node.JS (JSON.parse('null') => null or echo 'null' | python -mjson.tool => null.Daltondaltonism
echo '7' | python -mjson.tool yields 7, so I infer that tool apparently is willing to parse isolated JSON literal values as well as complete (object or array) JSON texts.Cribwork
Ruby’s JSON parser rejects just null as a value by default: ruby -r json -e 'JSON.parse("null")' givens “unexpected token at 'null' (JSON::ParserError)”Mollescent
Some parsers also choke on "[]".Seafarer
Note! This answer is no longer correct, see @Hickie answer belowColloquium
@moderators: (if any listening) Should we as community edit this answer with warning that it's outdated?Glimpse

© 2022 - 2024 — McMap. All rights reserved.