Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JSON values valid JSON?

Tags:

json

Are JSON values string, number, true, false, null valid JSON? I.e., is

true

a valid JSON document? Or does is have to be an array/object?

Some validators accept this (e.g. http://jsonlint.com/), while others do not (e.g. http://jsonschemalint.com/). The RFC and json.org are not clear on this issue.

like image 349
Jonny5 Avatar asked Mar 04 '13 16:03

Jonny5


People also ask

Can a JSON value be a JSON?

It is a common mistake to call a JSON object literal "a JSON object". JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format.

What counts as valid JSON?

A valid JSON object which is a list of key:value pairs separated by commas and enclosed between a left brace and right brace. A valid JSON value that is not a JSON array or a JSON object. A scalar value can be a character string, a number, or one of the JSON literals: null , true , or false .

Is undefined a valid JSON value?

undefined , Function , and Symbol values are not valid JSON values.


1 Answers

As of March 2014: Yes. From the specification:

A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array. Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts.

However, at the time this question was originally asked, the answer was: No. The original specification said:

A JSON text is a serialized object or array

So the outer-most data type in a JSON text had to be either an object or an array, it couldn't be a string, boolean, number or any other data type.

like image 108
Quentin Avatar answered Nov 10 '22 03:11

Quentin