Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON syntax for property names

What is the correct syntax to create objects in javascript that will work across the majority of web browsers (by that I mean : IE 6+, Firefox 2+, Opera 9+ )

Is this valid

var a = {   "class": "Person",   "name": "William Shakespeare",   "birthday": -12802392000000,   "nickname": "Bill" }; 

Or is this:

var a = {   class: "Person",   name: "William Shakespeare",   birthday: -12802392000000,   nickname: "Bill" }; 

And what is the difference between the two?

like image 990
Nikola Stjelja Avatar asked Dec 19 '08 12:12

Nikola Stjelja


People also ask

What is property name in JSON?

Objects are the mapping type in JSON. They map “keys” to “values”. In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”.

Do JSON objects have names?

JSON names are on the left side of the colon. They need to be wrapped in double quotation marks, as in “name” and can be any valid string. Within each object, keys need to be unique. JSON values are found to the right of the colon.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.


1 Answers

@AndreasN is correct: the JSON specification dictates the use of quotes in order for it to actually be JSON. If you don't use quotes, it may be a valid object literal in Javascript, but it is not JSON. Other services besides browser-side Javascript use JSON (e.g. webservices using php, Java, etc.) and if you construct a string that lacks the quotes, there is no guarantee that it will be parsed correctly -- although I suspect most implementations would be robust enough to do so.

FYI it is dangerous in Javascript to directly use eval() on JSON strings from sources which you cannot prevent malicious attacks. Again, see the JSON site which gives more of an explanation as well as a very short javascript file which safely parses JSON strings into Javascript objects.

edit: I guess technically your original question is not about JSON but rather the Javascript's syntax for object literals. The difference is that objects constructable from a JSON string will exclude many other possible object literals, e.g.:

var a = {cat: "meow", dog: "woof"}; var aname = {cat: "Garfield", dog: "Odie"}; var b = {   counter: 0,   pow: function(x) { return x+1; },   zap: function(y) { return (counter += y); } }; var c = {   all: [a,aname],   animals: a,   names: aname, }; 

Object literals "a" and "aname" can be expressed in JSON (by adding quotes to the property names). But object literals "b" and "c" cannot. Object literal "b" contains functions (not allowed in JSON). Object literal "c" above contains references to other variables in a way that is not representable in JSON because some of the references are shared. If you make a change to c.names it will also change c.all[1] since they share a reference to the same variable. JSON can only express objects that have a tree structure (e.g. each sub-element of the overall object is independent).

like image 167
Jason S Avatar answered Sep 22 '22 21:09

Jason S