145

per the debate in this post: json-conversion-in-javascript

asked Sep 30, 2010 at 17:42

6 Answers 6

175

Yes, an array is legal as top-level JSON-text.

There are four standard documents defining JSON: RFC 4627, RFC 7159 (which obsoletes RFC 4627), ECMA-404, and RFC 8259 (which obsoletes RFC 7159, and calls ECMA-404 normative). They differ in which top-level elements they allow, but all allow an object or an array as the top-level element.

  • RFC 4627: Object or array.
    "A JSON text is a serialized object or array."
  • RFC 7159, RFC8259: 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." Section 2
  • ECMA-404: Any JSON value.
    "A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar."
Sign up to request clarification or add additional context in comments.

4 Comments

As of this newer RFC, "A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names."
@antak What does this mean in relation to the question?
@OrestesKappa The comment is commentary on the answer and not the question, but it's spirit is now reflected in the current revision of this answer. Succinctly: The newer RFC does not limit JSON text to an object or an array and any serialized value is allowed.
Does the term "JSON text" refer to the top-level object? It's not clear to me.
73

Yes, but you should consider making the root an object instead in some scenarios, due to JSON hijacking. This is an information disclosure vulnerability based on overriding the array constructor in JavaScript.

answered Sep 30, 2010 at 17:56

1 Comment

5

This is from the ECMAScript specification.

JSONText :
 JSONValue
JSONValue :
 JSONNullLiteral 
 JSONBooleanLiteral 
 JSONObject 
 JSONArray 
 JSONString 
 JSONNumber
answered Sep 30, 2010 at 17:45

5 Comments

This is a little misleading, though, because ECMAScript allows you to parse JSON strings that are not top-level texts. According to the RFC, "A JSON text is a serialized object or array."
@Matthew - Weird, I wonder how Crockford feels about that. How will they reconcile the differences between the RFC and ECMA?
I just looked, and found they're aware of the difference. From ECMAScript 5 §15.12, "The top level JSONText production of the ECMAScript JSON grammar may consist of any JSONValue rather than being restricted to being a JSONObject or a JSONArray as specified by RFC 4627." I don't know if IETF will change the RFC.
@Matthew - Thanks for that, I was getting horribly confused. The json.org description doesn't mention the more restrictive concept of "json-text" at all, and the RFC's kind of vague about its significance.
This answer is about ECMAScript, but the question is about JSON. While they (deliberately) look similar, they are different specs.
4

Yes you can do it. Put in [{}]

Machavity
31.8k27 gold badges97 silver badges108 bronze badges
answered Sep 30, 2010 at 17:45

1 Comment

It's even easier than that. Put in [] and it will validate.
3

This works for me:

["apple","pear","banana"]

I store that above in MySQL DB as a string, and then have no problem doing this in my PHP:

 <?php
 $checkArr = json_decode($pageData[0]['pg_json']);
 echo $checkArr[0];
 ?>

Which yields this:

apple

answered May 31, 2022 at 6:51

Comments

3

There is some confusion, seen in the other comments. The "application/json" media type allows only object or array at the top-level for JSON-text, per JSON RFC. However, for a parser any JSON value is acceptable, as seen in the ECMAScript specification.

Update: RFC 4627 is outdated. The new RFC 7159 permits also simple values at the top-level. (Thanks, Matthias Dieter Wallnöfer.)

answered Jun 21, 2012 at 21:38

5 Comments

Any JSON value as the top-level element is acceptable for an ECMAScript parser, but not for a (compliant) JSON parser - important distinction.
That's an interesting distinction, but I don't understand what you're saying. What is the definition of a "(compliant) JSON parser"?
Well, a JSON parser is a parser for the JSON grammar. While JSON looks similar to Javascript, it is a different (much simpler) grammar. See tools.ietf.org/html/rfc7159 , which describes the JSON grammar. "compliant" just means the parser actually follows the grammar (which any decent parser should).
RFC 4627 is outdated, please don't follow it any longer. The new RFC permits also simple values at the top-level.
For future readers, the newer standard is RFC 7159.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.