1

I was learning JSON but Microsoft and w3school provide different information regarding the syntax.

I am stuck which one is correct.

According to microsoft

JSON:

 {firstName: "Rick", lastName: "Delorme", hairColor: "brown", eyeColor: "brown" }

Also According to Microsoft option D. is correct JSON syntax enter image description here

According to w3School

This is JSON

{ "name":"John" }

And this is java script-object

{firstName: "Rick", lastName: "Delorme", hairColor: "brown", eyeColor: "brown" }
Umang Patwa
  • 2,795
  • 3
  • 32
  • 41
  • Related: [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/q/2904131/218196) – Felix Kling Feb 09 '17 at 05:31
  • Yep, I noticed that after I commented. I edited the post to make the links more prominent. – Felix Kling Feb 09 '17 at 05:33
  • 1
    FWIW, the official website has examples: http://json.org/example.html . – Felix Kling Feb 09 '17 at 05:40
  • 1
    Microsoft's example is wrong, W3School's example is correct. Javascript's example is an object type so you can set it however you want as its an object not an actual json. – Desolator Feb 09 '17 at 05:45
  • The question from Microsoft seems wrong. The "D" option is JavaScript Object or least you can say the option D is more closer to correct JSON syntax because JSON and JavaScript object is very sikilar. i have mentioned the details in my answer. The actual JSON is W3Schools format. – Salman Lone Feb 09 '17 at 06:02
  • @Salman Lone Now according to Kim Gentes answer both syntax is valid – Umang Patwa Feb 09 '17 at 06:12
  • Both are valid in a sense you can use both. but you asked about the correct syntax which is officially declared is the one from w3schools. when you write { name: "xyz"} it on the back end convert into { "name" : "xyz" }. as i said JSON is a subset of JavaScript. – Salman Lone Feb 09 '17 at 06:19

7 Answers7

1

The syntax defined by W3Schools i.e { "name":"John" } is the correct one. You can validate any JSON through JSONLint. Try experimenting the both.

Muhammad Faizan Uddin
  • 1,339
  • 12
  • 29
  • Also according to W3Schools, correct Json for diffrent dataTypes : { "name":"John", "age":31, "city":"New York" }. Also yo ucan refer : http://json.org/example.html – Rahul Munjal Feb 09 '17 at 05:43
1

W3school's format is the correct format of writing the JSON ! If u want to pass only one parameter in JSON then u will use

{"name":"John"}

for multiple parameters it will be like,

{
   "user1" : {
      "name" : "mahesh",
      "password" : "password1",
      "profession" : "teacher",
      "id": 1
   },
   "user2" : {
      "name" : "suresh",
      "password" : "password2",
      "profession" : "librarian",
      "id": 2
   },
   "user3" : {
      "name" : "ramesh",
      "password" : "password3",
      "profession" : "clerk",       
      "id": 3
   }
}
Jaini
  • 90
  • 10
  • 1
    No, Microsoft's example is not correct. In JSON, keys must be strings (i.e. quoted). – Felix Kling Feb 09 '17 at 05:35
  • the example that i gave is correct one !! in rush, i didn't checked that multiple values are not in another String ! @FelixKling thanx for correcting me – Jaini Feb 09 '17 at 05:36
  • 2
    *"{firstName: "Rick", lastName: "Delorme", hairColor: "brown", eyeColor: "brown" } for multiple parameters"* is not valid JSON. And you claim *"both are the correct formats for writing JSON"*, which is wrong. – Felix Kling Feb 09 '17 at 05:36
  • @FelixKling according to Kim Gentes answer both syntax is valid is it true I am still not clear – Umang Patwa Feb 09 '17 at 06:22
  • @UmangPatwa: Well, that answer is wrong then. Look at the spec on http://json.org . It's clear that keys must be in quotation marks. To be clear: You are asking about *JSON*. JavaScript object literals look very similar (because JSON is *based* on that syntax), but are something completely different. – Felix Kling Feb 09 '17 at 06:25
1

Try to use JSONLint to validate your json , correct one is {"name":"John"} as per w3 schools.

check out this answer also.It may help.

Community
  • 1
  • 1
Gokul
  • 164
  • 1
  • 11
0

W3School syntax is correct one.

uday vichare
  • 61
  • 12
0

Read the link of w3schools and you will get the difference of JSON and JavaScript object.

This is the link of MSDN where you can verify the JSON syntax of Microsoft.

The JSON syntax is a subset of the JavaScript syntax.

JSON Syntax Rules

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON names require double quotes. JavaScript names don't.

In JSON, keys must be strings, written with double quotes: { "name":"John" }

In JavaScript, keys can be strings, numbers, or identifier names:{ name:"John" }

Salman Lone
  • 1,526
  • 2
  • 22
  • 29
  • @Teemu: *"object key names must be strings"* That statement is confusing. Object property names *are* strings, that much is true, but syntax wise, inside an object *literal*, the use of string *literals*, number *literals* and *identifier names* (and computed properties) is valid. – Felix Kling Feb 09 '17 at 06:26
  • @FelixKling Yes, you're right, bad choise of words from me. Even a function reference can be used as a property name, it is just converted to a string automatically. – Teemu Feb 09 '17 at 06:33
0

The short answer is that these are all following the same core formats, but there is one difference- JSON uses double quotes around names, while JavaScript does not. The distinction in the format specifications you have listed is the presentation of double quotes (") to surround the name portion of JSON object name/value pairs.

The official ECMA JSON Data Interchange Format document does not specifically say that the name portions of the name/value pair must be wrapped in quotation marks. See http://json.org/ as well for an even briefer summary. However, the JSON specifically does say that object notation in JSON is specifically defined by specifying a string before and a value after a colon (which constitutes the name/value pair), and both of those parts are to be surrounded by curly brackets :

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string. A single colon token follows each name, separating the name from the value (again, from ECMA JSON Data Interchange Format)

The specification further details that:

A string is a sequence of Unicode code points wrapped with quotation marks

Since the very definition of the object is a name/value pair, in which

"A name is a string" (section 6 of the same document)

One could infer that the name should always be encapsulated by quotes when referring to JSON (though not to JavaScript).

JSON itself literally means "JavaScript Object Notation", which means that its origination was within the scope of much JavaScript usage. In JavaScript itself, this:

{ name: "value portion" }

is practically equivalent to this:

{ "name": "value portion" }

The problem with always assuming that this is good practice when coding in JavaScript is that name portions of name/value pairs of a JSON string/object could possibly contain white space, such as

{ "name portion": "value portion" }

In this case, the following would be syntactically incorrect:

{ name portion: "value portion" }

For this reason, many people programming JavaScript choose to always use double quotes around the name portion of the name/value objects so that it maintains compatibility with JSON objects during construction or use.

Kim Gentes
  • 1,496
  • 1
  • 18
  • 38
  • 1
    *"Microsoft chose to expose the fact that nearly 100% of all JSON parsers will reasonably qualify the name portion of a name/value pair as a valid token so long as it does not contain whitespace."* That is wrong. You will get an error if you try `JSON.parse("{foo: 42}")` in every browser. – Felix Kling Feb 09 '17 at 06:24
  • Thanks @FelixKling . I've updated my post, since it was not only partially inaccurate, it was confusing with what I wanted to communicate. Appreciate that. – Kim Gentes Feb 11 '17 at 01:18
0

just in case

{
name:"Rishabh"}

Error: Parse error on line 1:
{   name: "Rishabh"}
--^
Expecting 'STRING', '}', got 'undefined'

hope u will get the point of using the double quotes at the Key/value pairs

Jaini
  • 90
  • 10