7

I have the following js array/object

var x = [1,2,3,4]; 
x.name = "myArray"; 

I am using json2.js and I am trying to serialize x in a string. and all I get is the array: [1,2,3,4]

is that correct ? since I can add any property to an array why doesn't json2 handle that ? what am I missing ?

4
  • 1
    what are you trying to do? Commented Apr 8, 2011 at 16:28
  • You cannot add any property to an array. Arrays are maps of numerical indices to values. Technically speaking in Javascript you can add any property to an Array object because they are built on top of the Object type, though you're not really supposed to. What is your intention? Commented Apr 8, 2011 at 16:28
  • 2
    Why overload the array? just do var x = {}; x.array=[1,2,3,4]; x.name = "myArray"; or make it JSON to begin with: var x={"name":"myArray","array":[1,2,3,4]} Commented Apr 8, 2011 at 16:29
  • 1
    You can add any named property to an array. It's just that there is no initialization syntax in JS to create an array with such named properties, and JSON is just the initialization syntax of JS. Commented Apr 8, 2011 at 16:55

4 Answers 4

5
  1. First of all json2.js ignores properties in the array.
  2. if it had not to ignore them then it would be impossible to have an array in json format which should be easy to eval.

Let's imagine we come out with something like:

[1,2,3,4]{name:'test',anotherProperty:'someValue'} 

if the above was valid javascript to create an array and stick two properties then it would be great and we could json-it. It would be equivalent to do this:

array = [1,2,3,4]
array.name = 'test';
array.anotherProperty = 'someValue'; 

But it is not and that's the reason why we can not persist into json.

Sign up to request clarification or add additional context in comments.

Comments

2

What were you expecting to see? :) I'm not sure what you're trying to do.

x is an array and all you did when you did x.name, you simply set up a property called name for the array object. The name property itself is not added into the array. You can see this in firebug:

enter image description here

The array object has a property called name, but the value of that property is not added to the array itself.

What you want to do is something like this:

var x = {
   array: [1, 2, 3, 4],
   name: "myArray"
};

Now you have an object called x that has two properties. The array property refers to an array, and the name property refers to the name. x.array will give you the array, and x.name will give you the name.

EDIT

This is in response to your comments. While it is true that arrays can be enriched with properties, think about what it means when you have to serialize that object. What exactly do you want to see when you serialize x? If x is already the array [1, 2, 3, 4], how would you represent it in JSON? You could represent it as:

{ 0: 1,
  1: 2,
  2: 3,
  3: 4,
  name: "myArray"
};

But what you now have is no longer an array. Also, the array object itself has a bunch of native properties. How should a JSON serializer handle those?

I think you're mixing up an array as an object and an array as a literal. An array as a literal is just [1, 2, 3, 4]. Now internally, the array as an object presumably has some property that points to the actual value of the array. It also has other properties (like length) and methods that act upon it (like slice).

The JSON serializer is concerned with the value of the object. If x is an array, then the thing that must be serialized is the value of x, which is [1, 2, 3, 4]. The properties of x are entirely different.

If you want to serialize these additional properties, then you would have to write your own serializer that will iterate over the properties of the array and represent everything in a map/associative array. This is the only way; the caveat, as I mentioned before, is that you no longer have an actual array.

4 Comments

Pedantry warning: it's just an object. The object literal is the means by which you created it.
@Tim Will fix. Sorry, got my terms mixed up!
array can be enriched with properties. in some situations this is very beneficial. There is no point here to say the situations. Let's say they are not the norm. Nonetheless I realize that enriched arrays are not json-able, which in my opinion is a shame.
x = {array:[1,2,3,4], name="test"} is completely different than x = [1,2,3,4]; x.name='test'; so why insisting ?
0

The problem is json2 still believes it's an array since x instanceof Array returns true. To fix it, you could create the "array" in a roundabout way:

var x = {
    0: 1,
    1: 2,
    2: 3,
    3: 4,
    length: 4,
    name: 'myArray'
};

The problem with this solution is that none of the array methods will be available on the object. So if you ever need to use pop, push, etc, you'll have to convert it to a real array with Array.prototype.slice.call(x); or if you're byte hungry [].slice.call(x);.

Comments

0

Not sure why you would do it, but a for in loop would get everything

var x = [1,2,3];
x.name = "wtf";
for(var y in x){
  console.log(y + ":\t" + x[y]);
}

Produces:

0: 1
1: 2
2: 3
name: wtf

but json2 code will not see it this way, why don't you convert it over to an object?

1 Comment

because the enriched array is passed to me and I need to persist it. I guess I could convert it before persisting it and when I deserialize it.

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.