1

I need to write a JSON string that will be interpreted as an array, but only certain indexes will actually have any values.

I would like an array that looks like this:

array[1] = ["foo", "bar"]
array[5] = ["things", "stuff"]
array[37] = ["etc"];

So that to return "stuff" I could call array[5][1].

The catch is that not everything will have values, and I'm writing this string by hand, so I don't really want to start from 0 and do all the empty values. Is there an easy way to do this?

Chris Sobolewski
  • 12,363
  • 12
  • 56
  • 95
  • If you don't want to sart from 0, then array[5][1] should return "things" in your example, right? I don't think this can be achieved by using a pure json object. – martincho Aug 30 '11 at 19:43
  • possible duplicate of [How to represent a sparse array in JSON?](http://stackoverflow.com/questions/1733658/how-to-represent-a-sparse-array-in-json) – James Montagne Aug 30 '11 at 19:43
  • Thanks, kingjiv, I didn't know the term "sparse array", so I was having trouble finding relevant results. Martin, the highest level of the array is the only one I care about. – Chris Sobolewski Aug 30 '11 at 19:45

2 Answers2

5

JSON representation of your data structure...

var data = {"1":{"0":"foo","1":"bar"},"5":{"0":"things","1":"stuff"},"37":{"0":"etc"}};

or just...

var data = {"1":["foo","bar"],"5":["things","stuff"],"37":["etc"]};
65Fbef05
  • 4,472
  • 1
  • 20
  • 24
3

If by "JSON string", you actually meant "object literal", then this answer will be useless. If you actually meant "JSON string", then all you need to do is create a string that can be parsed by JSON.parse() that has all of the characteristics of a sparse array and place your sub arrays at the positions in which you need them (by whatever means you are creating the string in the first place). Take a look at this example:

var array, 
    json = '[ null, ["foo", "bar"], null, null, null, ["things", "stuff"], ' + 
            'null, null, null, null, null, null, null, null, null, null, ' + 
            'null, null, null, null, null, null, null, null, null, null, ' + 
            'null, null, null, null, null, null, null, null, null, null, ' + 
            'null, ["etc"]  ]';


array = JSON.parse( json );

console.log(
    array[ 1 ][ 1 ] // "bar"
);

console.log(
    array[ 5 ][ 1 ] // "stuff"
);

console.log(
    array[ 37 ][ 0 ] // "etc"
);

See also: http://jsfiddle.net/rwaldron/DWCEb/

Edit. As a final note, considering your statement "The catch is that not everything will have values, and I'm writing this string by hand, so I don't really want to start from 0 and do all the empty values. Is there an easy way to do this?"

The answer is: No. You cannot have your cake and eat it, too.

If you have the means to create the array with code first, then you could easily do:

var array = [];

array[1] = ["foo", "bar"];
array[5] = ["things", "stuff"];
array[37] = ["etc"];

console.log(
    JSON.stringify( array )
);

See also: http://jsfiddle.net/rwaldron/RJHEp/

Rick
  • 1,376
  • 8
  • 11
  • in order to imitate the real js response, you should probably change the `null`s to `undefined` – gion_13 Aug 30 '11 at 20:06
  • Why define all the empty keys as `null` or `undefined` in the first place when you can explicitly define the keys: `var json = '{"1":["foo","bar"],"5":["things","stuff"],"37":["etc"]}';` – 65Fbef05 Aug 30 '11 at 20:27
  • @65Fbef05 because the OP _did_ _not_ ask for JSON string that would be interpreted as an object... "I need to write a JSON string that will be interpreted as an array". The `null` and/or `undefined` "holes" are used to simulate a sparse array. See also: http://en.wikipedia.org/wiki/Sparse_array – Rick Aug 30 '11 at 22:11
  • Arrays are objects in Javascript. `JSON.parse()` returns an object. – 65Fbef05 Aug 30 '11 at 22:14
  • @gion_13 Please see the live example I've just added. – Rick Aug 30 '11 at 22:14
  • @65Fbef05 I've **VERY** aware that Arrays are Objects in JavaScript, but thanks for the reminder. The point remains that the OP asked for "a JSON string that will be **interpreted** as an array" – Rick Aug 30 '11 at 22:15
  • For further clarification: Array.isArray( JSON.parse("[]") ); // true – Rick Aug 30 '11 at 22:20
  • He also said - _I don't really want to start from 0 and do all the empty values_. ;) – 65Fbef05 Aug 30 '11 at 22:22
  • That should've been addressed instead of trying to give the OP "sort of correct" information. – Rick Aug 30 '11 at 22:37