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

Johan
  • 35,120
  • 54
  • 178
  • 293
  • 3
    `JSON` means JavaScript Object Notation. That should pretty much answer your question, and also imply what came first. – Imp Aug 22 '12 at 19:53
  • Google is your friend. A simple guide: http://docs.1060.org/docs/3.1.0/book/discovered/doc_mod_json_guide.html – rgamber Aug 22 '12 at 20:00

5 Answers5

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
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.

Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
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.

Greg Ross
  • 3,479
  • 2
  • 27
  • 26
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"] }
Community
  • 1
  • 1
badunk
  • 4,310
  • 5
  • 27
  • 47