Is this simple string considered valid JSON?
Asked Answered
F

5

107

I've seen so many complicated questions in SO whether or not some complicated structure is considered to be valid JSON.

But what about something on the other end of the spectrum?

"12345"

Is the above valid JSON?

Fernald answered 20/9, 2011 at 15:40 Comment(2)
You may find this page usefull: jsonlint.comEurope
possible duplicate of What is the minimum valid JSON?Requirement
H
117

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

The confusion around this comes from Douglas Crockford's RFC 4627, which originally defined the application/json internet media type in 2006. It said that:

A JSON text is a serialized object or array.

However, as Crockford explained in a post in 2013 (unfortunately deleted with rest of Google+, but archived here):

JSON is just a grammar, and the grammar includes numbers and strings. Uses of JSON must necessarily be more restrictive. RFC-4627 is one possible use, and was never intended to be the standard for JSON itself.

The example string is a valid JSON value, but it would have been incorrect to use it as the full "JSON text" body of an application/json HTTP response. However, that's no longer true: RFC-4627 was obsoleted in 2014 with the publication of RFC 7159, which lets you use any JSON value:

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.

A "standard for JSON itself" was also published in 2013, as ECMA-404, and JSON was also defined in edition 5.1 of the ECMAScript (JavaScript) specification ECMA-262. These specifications and most parsers allow any JSON value as a complete JSON text, even if it's just a simple string.

Holozoic answered 20/9, 2011 at 15:41 Comment(0)
M
19

As of 2014, RFC 7159 obsoletes the older JSON RFCs, and declares that any JSON value is valid JSON text and valid application/json content - including strings. However, it also points out the incompatibility issue with older JSON implementations:

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.

More answered 30/10, 2015 at 16:22 Comment(0)
Y
6

Its a valid JSON string, but its not a JSON object.

See http://www.json.org/

Yonina answered 20/9, 2011 at 15:42 Comment(0)
F
6

At the time this question was written, this would not have been a valid JSON text. It would have been a valid string that could appear as part of a JSON text.

The original specification said:

A JSON text is a serialized object or array.

… meaning that the top level had to be {} or []. You couldn't dive straight in with a string.

The latest specification says:

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.

So now any value, including a string, can be a complete JSON text and "12345" is now valid.

Fourwheeler answered 20/9, 2011 at 15:56 Comment(0)
S
1

You can simply check what JSON.parse can handle:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#examples

This is all valid JSON:

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null
Sixfooter answered 4/11, 2021 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.