1

I'm a newbie to JS and JSON and trying to understand the difference, I see other threads on this difference but still have few unanswered questions,

I have created 3 objects

  1. Key-vaue pairs in double quotes
  2. Key without quotes but value with quotes
  3. Key-value pairs in single quotes.

Questions.

  1. Asis, is it safe to assume if all the 3 objects are Javascript objects?
  2. How do I determine which one is JSON Object here, when I print the objects in the log, all the objects looks same. Is there a way to determine the JSON Object?
  3. If JSON Objects - key-value pairs are enclosed in double quotes, what does the single quote mean?

Code:

<html>
    <head>
        <script>
            var jsobject = {"fname":"Bob","lname":"Mike"}
            console.log(jsobject)

            var jsobject = {fname:"Bob",lname:"Mike"}
            console.log(jsobject)

            var jsobject = {'fname':'Bob','lname':'Mike'}
            console.log(jsobject)

        </script>
    </head>
    <body>
    </body>
</html>
24
  • 1
    @susheel: There's a huge difference. They just happen to use an overlapping syntax. Commented Dec 27, 2013 at 18:37
  • 2
    1) Yes, all 3 are JavaScript objects. 2) To JavaScript, JSON is a String representation, which is why JSON.stringify() returns a String and JSON.parse() expects a String. 3) In JavaScript object literals/initializers, identifiers, numbers, and both single- and double-quoted strings can all be used as keys. JSON uses stricter syntax and only allows double-quoted strings. Commented Dec 27, 2013 at 18:37
  • 1
    you don't have any JSON in your code, just three different yet equivalent object literal formats. only static code analysis could spot the difference. JSON uses JSON.parse/eval to go live, i don't see anything like that here... Commented Dec 27, 2013 at 18:38
  • 2
    susheel: yeah, that's a good read. @MehranHatami what do you mean by "there is no difference"? JSON is serialized data in string form, that's completely different from JS objects. Commented Dec 27, 2013 at 18:49
  • 1
    @MehranHatami No. The notation is similar - sure it was based in the JS objects notation, however the term "JSON" refers to a specification which defines a much stricter subset of the JS object notation. JSON is used as a data interchange language and can only occur in string context. I'd suggest reading up the link susheel sent, it's very enlightening: There's no such thing as a "JSON Object" Commented Dec 27, 2013 at 18:59

1 Answer 1

5

I think you're confusing syntax and data.

Any number of technologies can have very similar syntax, yet that similar syntax may be used for entirely different purposes, and for creating vastly different data.

when we talk about JSON, we're talking about textual data with a Unicode encoding that follows a character syntax that is meant to be used as a data transferral mechanism. That JSON data can be transferred into a wide variety of different programming environments, parsed, and then converted into actual object structures that make sense for the environment.

The reason that it was named "JavaScript Object Notation" is that its notation is largely patterned after a subset of the literal syntax used in JavaScript programs to create objects and primitive values. Sadly, this naming contributes to the confusion of JavaScript developers.

So to determine if you're dealing with JSON, what's ultimately the most important thing to think about is whether what you're doing will result in the creation of Unicode data that follows the rules of JSON syntax.

Take this example:

var foo = {"bar":"baz"};

Is that JSON? Well if it runs in a JavaScript program, it will be evaluated, and foo will hold a reference to some memory that is not Unicode text data.

Sure we could isolate the {"bar":"baz"} part of the code, and transfer it into its own text file that is encoded as Unicode, but then we're really not dealing with the same example anymore.

So let's say we did that. We open our text editor, make sure it's set up for Unicode encoding, and then paste in that one part of the above code. So now the entirety of our text file is this:

{"bar":"baz"}

Now we can correctly say that we have JSON data. What if I added a ; to the end?

{"bar":"baz"};

It's no longer JSON because it has been corrupted by the ; which is not allowed. Again, we could play around with calling it JSON except for whatever isn't valid, but really it either is or isn't valid in its entirety.

So back to a JavaScript example. Does it every make sense to refer to JSON within the syntax of a JavaScript program? Well take our original example. If we could use some JavaScript syntax to create Unicode data and make it comply with JSON syntax, then yes, we could correctly speak of having JSON in our program.

So does JavaScript let us create Unicode data? Yes, all strings in JavaScript are UTF-16 encoded. Therefore all we need to do is create a string.

var foo = '{"bar":"baz"}';

Now we wouldn't call that entire line JSON, but we could correctly say that the foo variable refers to memory which does hold JSON data.

We could then transfer that data to a server written in an entirely different programming language, and as long as it has a JSON parser, it could parse it, and convert it to whatever object type makes sense to that server.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.