-1

Looking at json spec, I'm not sure whether a value is permitted to stand alone, or is only permitted as part of object or array structure.

This is valid JSON:

[123]

But is this valid JSON:

123
frnhr
  • 12,354
  • 9
  • 63
  • 90
  • Just try `JSON.parse(123)` in the console. –  Jul 16 '16 at 16:26
  • @torazaburo I tried several online parsers and validators, and about half of them said it was not valid JSON. They are presumably using prominent libraries for server side languages, yet they differ. I would be not at all surprised if different browsers gave different answer in their console. Hence my asking about the *spec*. – frnhr Jul 16 '16 at 16:34
  • Which online validator(s)? –  Jul 16 '16 at 16:43
  • See http://stackoverflow.com/questions/13318420/is-a-single-string-value-considered-valid-json. –  Jul 16 '16 at 16:44
  • @torazaburo e.g. http://json.parser.online.fr/ https://jsonformatter.curiousconcept.com/ – frnhr Jul 16 '16 at 16:48

2 Answers2

0

A value is permitted to stand alone.

The rule has been changed a few years ago.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • You'd have to show me an outdated reference with the "old rule" to convince me it was ever otherwise. In the many years I've been working with JSON, it has always allowed for values to stand alone (outside of an object or array). – JAAulde Jul 16 '16 at 15:50
  • _(not that you're required to convince me ;) )_ – JAAulde Jul 16 '16 at 15:51
  • @JAAulde Please look at [rfc4627](https://tools.ietf.org/html/rfc4627). It's also mentioned in the Wikipedia article. – vadian Jul 16 '16 at 15:57
  • I see what you're saying, but I believe it is pretty ambiguous. I'd say latter documents are clarification as opposed to change. I could very well be wrong, though. Cheers! – JAAulde Jul 16 '16 at 16:20
0

Per ECMA-404, The JSON Data Interchange Standard (pdf), which is linked from the JSON.org page you linked:

A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar.

And:

A JSON value can be an object, array, number, string, true, false, or null.

As such, the value 123 is valid JSON, representing an integer.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • 1
    `... that conforms to the JSON value grammar.` - that is precisely the important bit that was wondering about, thanks! – frnhr Jul 16 '16 at 15:48
  • I didn't realize that "ECMA-404, The..." is actually a link! :)) Usability failure: visited links are colored BROWN! – frnhr Jul 16 '16 at 16:24