I have seen a tutorial that has the following JSON:
{
books:[{title:"frankenstein"},{title:"Moby Dick"}]
}
When I put the above into js.bin I get an error saying that it expect a string and saw books. So I changed it to the below and it works…
{
"books":[{"title":"frankenstein"},{"title":"Moby Dick"}]
}
However if I change the first code snippet from above to the below it works.
var p = {
books:[{title:"frankenstein"},{title:"Moby Dick"}]
};
What im wondering is:
1) Do you really have to use speech marks for the variable name in JSON (the example I saw is incorrect if you do).
2) Is there a relationship between JSON and javascript object notation?
Yes.
Yes. JSON is an abbreviation of JavaScript Object Notation (but I don’t think that is what you mean).
The relationship between JSON and JavaScript is that JSON is (very nearly) a subset of the syntax used to create literals in JavaScript.
JSON is heavily simplified which makes it slightly less convenient to handcraft (don’t handcraft JSON, use a library!) but makes the specification smaller and parsers easier to write.
Most of the simplifications are down to exceptions being removed.
For example:
In JavaScript, an object literal key can be an identifier (then insert a detailed explanation about what makes a valid identifier in JS here) or a string.
In JSON an object key must be a string.
In JavaScript, a string may be delimited by either single or double quotes.
In JSON, a string must be delimited by double quotes.