I've carefully read the JSON description http://json.org/ but I'm not sure I know the answer to the simple question. What strings are the minimum possible valid JSON?
"string"
is the string valid JSON?42
is the simple number valid JSON?true
is the boolean value a valid JSON?{}
is the empty object a valid JSON?[]
is the empty array a valid JSON?
The ecma specification might be useful for reference:
http://www.ecma-international.org/ecma-262/5.1/
JSON stands for JavaScript Object Notation. Only
{}
and[]
define a Javascript object. The other examples are value literals. There are object types in Javascript for working with those values, but the expression"string"
is a source code representation of a literal value and not an object.Keep in mind that JSON is not Javascript. It is a notation that represents data. It has a very simple and limited structure. JSON data is structured using
{},:[]
characters. You can only use literal values inside that structure.It is perfectly valid for a server to respond with either an object description or a literal value. All JSON parsers should be handle to handle just a literal value, but only one value. JSON can only represent a single object at a time. So for a server to return more than one value it would have to structure it as an object or an array.
Just follow the railroad diagrams given on the json.org page. [] and {} are the minimum possible valid JSON objects. So the answer is [] and {}.
At the time of writing, JSON was solely described in RFC4627. It describes (at the start of "2") a JSON text as being a serialized object or array.
This means that only
{}
and[]
are valid, complete JSON strings in parsers and stringifiers which adhere to that standard.However, the introduction of ECMA-404 changes that, and the updated advice can be read here. I've also written a blog post on the issue.
To confuse the matter further however, the
JSON
object (e.g.JSON.parse()
andJSON.stringify()
) available in web browsers is standardised in ES5, and that clearly defines the acceptable JSON texts like so:This would mean that all JSON values (including strings, nulls and numbers) are accepted by the JSON object, even though the JSON object technically adheres to RFC 4627.
Note that you could therefore stringify a number in a conformant browser via
JSON.stringify(5)
, which would be rejected by another parser that adheres to RFC4627, but which doesn't have the specific exception listed above. Ruby, for example, would seem to be one such example which only accepts objects and arrays as the root. PHP, on the other hand, specifically adds the exception that "it will also encode and decode scalar types and NULL".There are at least four documents which can be considered JSON standards on the Internet. The RFCs referenced all describe the mime type
application/json
. Here is what each has to say about the top-level values, and whether anything other than an object or array is allowed at the top:RFC-4627: No.
Note that RFC-4627 was marked "informational" as opposed to "proposed standard", and that it is obsoleted by RFC-7159, which in turn is obsoleted by RFC-8259.
RFC-8259: Yes.
RFC-8259 is dated December 2017 and is marked "INTERNET STANDARD".
ECMA-262: Yes.
ECMA-404: Yes.
Yes, yes, yes, yes, and yes. All of them are valid JSON value literals.
However, the official RFC 4627 states:
So a whole "file" should consist of an object or array as the outermost structure, which of course can be empty. Yet, many JSON parsers accept primitive values as well for input.