T J
T J

Reputation: 43156

Is there any difference between specifying a json's key with and without quotes?

Is there any difference between the following title keys?

jsonObj = {
  title: "hello"
}

and

jsonObj = {
 "title": "world"
}

Upvotes: 1

Views: 80

Answers (2)

deceze
deceze

Reputation: 522145

As a Javascript object literal (as Javascript code), they're identical.
As the JSON data format, only with quotes is valid JSON.

JSON is a strict subset of Javascript syntax, they're not the same thing.

Upvotes: 3

DhruvPathak
DhruvPathak

Reputation: 43245

Both are valid javascript objects, but only second one is a valid JSON object. Preferably use the second one, that also helps when property name is a keyword, or has special characters in it .e.g ":" or space.

jsonObj = {
  "my second key::second range":99
}

Upvotes: 2

Related Questions