The strict difference between JSON and JavaScript literals is only how you use it.
JSON is a text format to represent data. JavaScript literals is a part of JavaScript code.
You can't use JSON in JavaScript code, because then it's by definition not JSON any more, it's JavaScript. You can take JSON and use as JavaScript code, however. You can also have JSON inside a string in JavaScript.
JSON syntax is a subset of JavaScript literals syntax, so you can take any JSON and use as JavaScript, but you can't take any JavaScript literal and use as JSON.
This is an example of JSON:
[{
"todo": {
"title": "title",
"content": "lorem"
}
}]
You can use that as JavaScript:
var arr = [{
"todo": {
"title": "title",
"content": "lorem"
}
}];
You can also have the JSON as a string and parse it to a JavaScript value:
var json = '[{ "todo": { "title": "title", "content": "lorem" } ]';
var arr = JSON.parse(json);
Note that parsing JSON does't use the JSON as JavaScript, it reads the JSON and creates JavaScript values that corresponds to it. (Before JSON parsing was available the eval
function was used to parse JSON, and that would actually use the JSON as JavaScript. JSONP requests still use JSON as JavaScript, as it loads the data using a script tag.)
JavaScript is less strict and doesn't require delimiters for object property names that can be used as identifier. Also, you can use expressions when creating JavaScript literals:
var arr = [{
todo: {
title: document.title,
content: "lo" + "rem"
}
}];
If you take that JavaScript literal and try to use as JSON, the syntax is wrong.