Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a single string value considered valid JSON? [duplicate]

Tags:

json

Do you consider the JSON web response:

"A serialization error occurred" 

to be valid or not?

Some validators accept it while others don't.

like image 558
Sylvain Demers Avatar asked Nov 10 '12 01:11

Sylvain Demers


People also ask

Is a string value valid JSON?

Yes, in most contexts. It is valid JSON syntax representing a JSON value.

What is a valid JSON string?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.

Can JSON have duplicate values?

We can have duplicate keys in a JSON object, and it would still be valid. The validity of duplicate keys in JSON is an exception and not a rule, so this becomes a problem when it comes to actual implementations.


2 Answers

As for new JSON RFC, json, containing only single value is pretty valid.

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.

like image 120
igor Avatar answered Sep 28 '22 00:09

igor


There's a change of heart on this between RFC4627 and RFC7159:

RFC4627:

A JSON text is a serialized object or array.

  JSON-text = object / array 

RFC7159:

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.

  JSON-text = ws value ws 

No philosophical or practical justification is provided for this change of heart. The earlier version probably makes more sense as it consistently dictates that both a singe list element and a single map element (a pair or tuple) be contained. The second version allows only a single list element to be uncontained.

like image 35
Basel Shishani Avatar answered Sep 28 '22 00:09

Basel Shishani