4

I'm trying to stringify a multi-array variable into a JSON string in Javascript. The

//i'm using functions from http://www.json.org/json2.js

var info = new Array(max);
for (var i=0; i<max; i++) {
  var coordinate = [25 , 32];
  info[i] = coordinate;
}
var result = JSON.stringify(info);

But result doesn't look like a JSON string at all. What am I doing wrong here?

1
  • 6
    What does the result look like then? Commented Jun 10, 2009 at 9:13

2 Answers 2

9

You, and many in this question, are confused about the JSON specification. The string you have is a valid JSON array.

From json.org

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Your example matches the second structure - the ordered list of values.

Also from json.org:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Doesn't leave much to the imagination. You've got a valid JSON array there. Have fun with it. But just to be annoyingly thorough:

From the RFC

RFC 4627, Section 2

2) JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

  JSON-text = object / array
Sign up to request clarification or add additional context in comments.

1 Comment

Just chiming in to point out that this has since been obseleted by RFC 7159. A JSON text no longer needs to be an object or array. tools.ietf.org/html/rfc7159#appendix-A
7

The result looks like this for me:

[[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32]]

Which is fine as far as I can see. It might look a bit weird, but that is mostly because JSON is used a lot for objects, which have a slightly different notation. You can eval the string and get the array structure back though, so it looks fine to me.

10 Comments

JavaScript arrays are objects. However, JSON serializes them specially, just like they have their own literal syntax.
From json.org: An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma). So yes, it looks like the correct JSON representation of a two-dimensional array to me.
@annakata, this is clearly valid JSON. I'm not sure how anyone could disagree. Check it out at json.org (homepage)
@ylebre - I think the confusion was that some didn't believe an array could be a top-level JSON-text (to use terminology from the RFC).
RFC says that an array is legal top-level JSON-text. ietf.org/rfc/rfc4627.txt?number=4627
|

Your Answer

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.