47

Do you consider the JSON web response:

"A serialization error occurred"

to be valid or not?

Some validators accept it while others don't.

3
  • What response are you talking about? Could you possibly share that?
    – Srinivas
    Commented Nov 10, 2012 at 1:45
  • I was refering the the single string value: "A serialization error occurred" Commented Nov 12, 2012 at 13:04
  • Make sure your string is escaped with quotes (""), i.e. its not just A serialization error occurred but "A serialization error occurred"
    – tuxSlayer
    Commented Aug 14, 2014 at 13:08

5 Answers 5

41

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.

2
  • 1
    Yes, it is valid. Note that for better interoperability you can return a single element array: [ "value" ] Commented Feb 12, 2015 at 13:55
  • For reasons of extensibility, one should always use an object, especially when dealing with a Rest API.
    – devops
    Commented Jan 18, 2022 at 7:21
31

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.

19

According to the grammar exposed in http://www.json.org/ (which references the Standard ECMA-262 3rd Edition - December 1999 par.5.1.5 The JSON Grammar) it's wrong:

The initial element must be:

enter image description here

enter image description here

and then a value can be a string:

enter image description here

2
  • 1
    Hi Tony, I did consult that page too, and I had the same understanding. But I wanted to ask the community since some sites (like the one I putted as reference) consider the single string value as a valid json response. Seeing the responses here just confirm that is does not make sense. Commented Nov 12, 2012 at 13:08
  • 1
    I tried that site and I agree with you it seems misleading. Usually I use jsonlint.com which is not accepting a string as a valid JSON: "Parse error on line 1: "ciao" ^ Expecting '{', '[' " . The error message is clear: it is expecting an Object or an Array.
    – Tony Rad
    Commented Nov 12, 2012 at 15:56
1

From RFC4627:

A JSON text is a serialized object or array.
    JSON-text = object / array

IE, the root element has to be an object or array, and can't be a string value by itself.

0
-4

I don't care if some validator accepts it. It's wrong. It's a question of good practice, Json format must be {"key": "value", .....}. If you consider that text Json, can work, but for the rest of programmer it's not a serious Json. If you use only that text, then you don't need Json.

2
  • Not that simple. New specifications consider single values as valid JSON. Commented Feb 12, 2015 at 13:53
  • 3
    Consider the case of an JSON API, a very common case. When returning a 404 or other error how should the value be encoded in your world? I would also suggest that "serious Json" (surely you mean "serious JSON"?) is what the JSON spec defines. For the internet and the content type application/json that is RFC-7159 (tools.ietf.org/html/rfc7159) and it allows for a pure string, specifically: "JSON-text = ws value ws [...] A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true". Commented Aug 1, 2015 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.