0
var foo = { "a": 1, "b": true, c: [1, true, "2"] };

Please correct me if I'm wrong here, but as far as I know, this is a valid json object. But it's also a javascript object.

Are JSON objects based on the javascript language? Or is it the other way around?

Thanks

2
  • 3
    JSON means JavaScript Object Notation. That should pretty much answer your question, and also imply what came first. Commented Aug 22, 2012 at 19:53
  • Google is your friend. A simple guide: docs.1060.org/docs/3.1.0/book/discovered/… Commented Aug 22, 2012 at 20:00

5 Answers 5

4

Are JSON objects based on the javascript language?

Yes.

See the specification:

JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard.

Note that ECMAScript is the standardised version of JavaScript.

1

"Please correct me if I'm wrong here, but as far as I know, this is a valid json object"

No.

"But it's also a javascript object."

yes.

UPDATE: my original answer continues below, but I missed an important syntax error which is helpfully pointed out by @badunk

The string

{ "a": 1, "b": true, c: [1, true, "2"] }

is JSON. JSON is just about notation - about which symbols make up valid syntax, and what they mean if they are processed.

Your code:

var foo = { "a": 1, "b": true, c: [1, true, "2"] };

..is a piece of javascript. When this is parsed and processed, the part on the right side of the assignment is called a javascript object literal. That is, a piece of javascript that denotes a literal object. But because it is in fact an object, it is not notation anymore - it is processed into a runtime data structure.

The term JSON is useful when you're talking about data exchange, for instance over HTTP. If a HTTP response passes a sting like this:

{ "a": 1, "b": true, c: [1, true, "2"] }

it is valid JSON. If that would be interpreted, it would result in a javascript object.

1

from the JSON website

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language.

So the answer to your question will be yes.

1

One can argue that there's no such thing as a "JSON object" (http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/).

Your code, above, is in fact an object literal. JSON, on the other hand, is just a string representation of an object, i.e. it's a serialisation.

1

as @Roland pointed out, the statement itself is a javascript expression, not JSON. Ignoring that, however, and evaluating whether the following is JSON:

{ "a": 1, "b": true, c: [1, true, "2"] }

I disagree with the other answers here, this is not valid JSON. Strictly speaking, properties must be enclosed in quotes. See the SO post here. This is correct JSON:

{ "a": 1, "b": true, "c": [1, true, "2"] }
1
  • And right you are. I missed that. Commented Aug 22, 2012 at 20:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.