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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Sylvain Demers
  • 481
  • 1
  • 4
  • 3

5 Answers5

39

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.

user247702
  • 23,641
  • 15
  • 110
  • 157
igor
  • 2,746
  • 3
  • 20
  • 24
  • 1
    Yes, it is valid. Note that for better interoperability you can return a single element array: `[ "value" ]` – Filipe Borges Feb 12 '15 at 13:55
  • For reasons of extensibility, one should always use an object, especially when dealing with a Rest API. – alex Jan 18 '22 at 07:21
30

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.

Community
  • 1
  • 1
Basel Shishani
  • 7,735
  • 6
  • 50
  • 67
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

Tony Rad
  • 2,479
  • 20
  • 32
  • 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. – Sylvain Demers Nov 12 '12 at 13:08
  • 1
    I tried that site and I agree with you it seems misleading. Usually I use http://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 Nov 12 '12 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.

fgb
  • 18,439
  • 2
  • 38
  • 52
-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.

Srinivas
  • 1,780
  • 1
  • 14
  • 27
itaka
  • 399
  • 1
  • 5
  • 16
  • Not that simple. New specifications consider single values as valid JSON. – Filipe Borges Feb 12 '15 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 (https://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". – Jonas Schubert Erlandsson Aug 01 '15 at 09:33