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?

3
  • 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. Commented Aug 30, 2011 at 19:43
  • possible duplicate of How to represent a sparse array in JSON? Commented Aug 30, 2011 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. Commented Aug 30, 2011 at 19:45

2 Answers 2

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"]};
Sign up to request clarification or add additional context in comments.

2 Comments

tho, he wants numerical indices, so maybe var data = {1: ["foo", "bar"], 5: ["things", "stuff"], 37: ["etc"]};
the javascript object's keys are string. check it out yourself: a={1:'numeric',"1":'string'};alert(a[1]);
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/

9 Comments

in order to imitate the real js response, you should probably change the nulls to undefined
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 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: en.wikipedia.org/wiki/Sparse_array
Arrays are objects in Javascript. JSON.parse() returns an object.
@gion_13 Please see the live example I've just added.
|

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.