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?

Nosredna
  • 83,000
  • 15
  • 95
  • 122
Hectoret
  • 3,553
  • 13
  • 49
  • 56

2 Answers2

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
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
  • 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. https://tools.ietf.org/html/rfc7159#appendix-A – stewSquared Mar 15 '18 at 22:04
6

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.

ylebre
  • 3,100
  • 1
  • 18
  • 14
  • JavaScript arrays are objects. However, JSON serializes them specially, just like they have their own literal syntax. – Matthew Flaschen Jun 10 '09 at 09:26
  • 3
    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. – ylebre Jun 10 '09 at 09:31
  • Seriously, this is not JSON - it's a valid *value* of a property within a JSON object maybe, but it's not valid JSON as written. You need to reread the specs. – annakata Jun 10 '09 at 09:33
  • According to the spec, how should a two-dimensional array with integers should look like in JSON then? I'm starting to wonder here. – ylebre Jun 10 '09 at 09:45
  • 4
    @annakata, this is clearly valid JSON. I'm not sure how anyone could disagree. Check it out at json.org (homepage) – Kenan Banks Jun 10 '09 at 20:23
  • 2
    @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). – Kenan Banks Jun 11 '09 at 00:16
  • @Triptych: that is what I figured - I also figured the rest of stackoverflow would help with the correct answer, which they did :) – ylebre Jun 11 '09 at 07:43
  • 5
    RFC says that an array is legal top-level JSON-text. http://www.ietf.org/rfc/rfc4627.txt?number=4627 – Dustin Getz Sep 30 '10 at 17:22
  • There is a difference between the "application/json" media type and ECMAScript. The former is a strict subset of the latter. See [this question](http://stackoverflow.com/questions/3833299/can-an-array-be-top-level-json-text). – cdunn2001 Jun 21 '12 at 21:56