0

Well I have always used

var j= {};
j['field'] = value;

Apparently below works too

var j= [];
j['field'] = value;

Any difference?

Edit: Sorry I guess I used the wrong word json here instead of object. My bad. But my questions till stands. Most people are explaining the difference between an array and object to me which I am familiar with.

I always do var x = []; x.push('blah') etc.

I also do var x = {}; x.Name = 'Michael'; x.Age = 21 etc.

What I was surprised to see and what prompted me to ask this is I saw code that goes like var x = []; x.name = 'Michael';

This is something I wasn't used to and hence posted the question.

Thanks for all the answers. I have marked accepted answer which addressed my question the best

Jubin Jose
  • 59
  • 1
  • 2
  • 8
  • 16
    This is not JSON, this is JavaScript code. `[]` denotes an array. `{}` denotes an object. Those are two different data structures. Tutorials should cover such things: http://eloquentjavascript.net/chapter4.html. Never use arrays with string keys, it does not work as you might think (such elements are not part of the array). It "works" because arrays are objects too, a special kind of object. – Felix Kling Jun 04 '13 at 15:39
  • Well, one creates an `Array` instance, the other an `Object` – techfoobar Jun 04 '13 at 15:39
  • @FelixKling, I just love how your's comments get more upvotes than the answers! says a lot about you! – gdoron Jun 04 '13 at 15:45
  • comment votes are cheap ;-) – Alnitak Jun 04 '13 at 15:46
  • @gdoron: Maybe it means I should write more answers and less comments ;) Thanks though :) – Felix Kling Jun 04 '13 at 15:46
  • @FelixKling: "such elements (string keys) are not part of the array" Not necessarily true: `[1,2,3,4]["2"] === 3` – recursive Jun 04 '13 at 15:48
  • @recursive: I meant non-numeric properties. All property names are strings anyways ;) I often casually say "strings" because I doubt anyway would use numeric *string literals* in source code. – Felix Kling Jun 04 '13 at 15:49
  • @FelixKling, or maybe it means we shouldn't be after the rep all the time, and not all answers should be kept alive and get answered. – gdoron Jun 04 '13 at 15:50
  • 3
    @gdoron: That's a good point. I often write a comment to a bad question to give at least *some* help, but vote to close the question. Too many low-quality questions get answered IMO. *note:* I'm not saying *this* is a bad question, I don't want to judge. It's basic, yes, and should be answerable by reading documentation, but it's not necessarily bad. – Felix Kling Jun 04 '13 at 15:52

7 Answers7

9

Your objects aren't "JSON", they're (empty) object and array literals. JSON is the serialized form you get by passing such a value of JSON.stringify.

The latter won't work if you do try to convert it to JSON, even though it's legal JavaScript:

var json = [];             // ok, it's an array
json["field"] = "value";   // still OK - arrays can have properties too
JSON.stringify(json);
> "[]"

i.e. you just get an empty array, because when you try to serialize an array you only get out the numerically indexed properties from 0 to myArray.length - 1, and not named properties.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Thanks. That explains. Being a seasoned web developer sometimes it is embarrassing when you come across such doubts. But I had to get it cleared anyways! – Jubin Jose Jun 04 '13 at 19:39
3

[] is an array initializer

{} is an object initializer

Jason
  • 15,915
  • 3
  • 48
  • 72
3
  • [] is a zero length array literal.

  • {} is an empty object literal.

recursive
  • 83,943
  • 34
  • 151
  • 241
1

[] creates an Array instance (which is an object instance with certain additional properties/methods), {} an Object instance.

The one created using [] will have methods from Array's prototype like push(), slice() etc. whereas the second one won't.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
1

[] is array, and {} is literal object.

You can do both things because both are JavaScript primitive objects.

When you do it in an array, you're setting an property in your array object, and you're not adding an item to it.
When you do it in the literal object... well, as said above, you're setting an property in that object.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
1

There is no difference.

In both cases you are setting a property on an object. It is just that the second object is an array.

In other words, in your latter example, you effectively use the same technique that you use in your former example, that is, you are setting a property on an object. in this case, an array. But you are not changing the contents of the array.

akonsu
  • 28,824
  • 33
  • 119
  • 194
1

Below is an javascript Object,

var json = {};
json['field'] = value; //Valid

Below is an javascript Array,

var json = [];
//Below does not set the value in array instead it sets a property name field.
//json['field'] = value; 
json.push(value);      //proper way to add an element to an array

Both are Javascript objects and nothing to do with JSON yet.

JSON is a notation, a convention followed across different language for easy data communication. Basically the above will be transformed to a string literal following JSON convention which can be read across different language.

There are two methods,

  1. JSON.stringify - Convert the above to string literal following JSON notation.
  2. JSON.parse - Convert the JSON string literal to a valid javascript object.

The same function are available as a library in most languages which allows you to communicate across different language easily.

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134