0

i cant see the difference between:

self.todos = [{
    "todo": {
        "title": "title",
        "content": "lorem"
    }
}, {
    "todo": {
        "title": "title",
        "content": "lorem"
    }
}];

and

    self.todos = [{
    todo: {
        title: "title",
        content: "lorem"
    }
}, {
    todo: {
        title: "title",
        content: "lorem"
    }
}];

both work in my code but only the first veryfies JSON (online JOSON veryfier). Is there a preference what to use.

thanks a lot

user2344231
  • 35
  • 1
  • 5

2 Answers2

4

If you're writing JSON, you must conform to the rules for JSON, which include the fact that the property names must be in double quotes. (And that strings must be in double, not single quotes, and there's no undefined or functions, and various other things...)

What you're writing clearly isn't JSON, however, it's JavaScript code; I can tell from the self.todos = part. So you can use no quotes, single quotes, or double quotes on the property names; it's up to you. There are some property names where you need quotes, for instance when the property name has a space:

var o = {
    "I have a space": 42
};

The key distinction is that JavaScript source code is source code, and JSON is a textual notation for data exchange which is a simplified version of one small part of JavaScript source code. When you're dealing with JavaScript source code, you're not dealing with JSON, unless you're dealing with strings (e.g., a string containing JSON, which you might parse or send to a server or similar).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005