Can someone tell me what is the main difference between a JavaScript object defined by using Object Literal Notation and JSON object?
According to a JavaScript book it says this is an object defined by using Object Notation:
var anObject = { property1 : true, showMessage : function (msg) { alert(msg) } };
Why isn't it a JSON object in this case? Just because it is not defined by using quotation marks?
It is a common mistake to call a JSON object literal "a JSON object". JSON cannot be an object. JSON is a string format. The data is only JSON when it is in a string format.
Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script.
There is really no such thing as a "JSON Object". The JSON spec is a syntax for encoding data as a string.
Lets clarify first what JSON actually is. JSON is a textual, language-independent data-exchange format, much like XML, CSV or YAML.
Data can be stored in many ways, but if it should be stored in a text file and be readable by a computer, it needs to follow some structure. JSON is one of the many formats that define such a structure.
Such formats are typically language-independent, meaning they can be processed by Java, Python, JavaScript, PHP, you name it.
In contrast, JavaScript is a programming language. Of course JavaScript also provides a way to define/describe data, but the syntax is very specific to JavaScript.
As a counter example, Python has the concept of tuples, their syntax is (x, y)
. JavaScript doesn't have something like this.
Lets look at the syntactical differences between JSON and JavaScript object literals.
JSON has the following syntactical constraints:
"
). true
false
null
{"foo":"bar","foo":"baz"}
) produce undefined, implementation-specific results; the JSON specification specifically does not define their semanticsIn JavaScript, object literals can have
undefined
.Knowing that, just by looking at the syntax, your example is not JSON because of two reasons:
But most importantly, to repeat my explanation from the beginning: You are in a JavaScript context. You define a JavaScript object. If any, a "JSON object" can only be contained in a string:
var obj = {foo: 42}; // creates a JavaScript object (this is *not* JSON) var json = '{"foo": 452}'; // creates a string containing JSON
That is, if you're writing JavaScript source code, and not dealing with a string, you're not dealing with JSON. Maybe you received the data as JSON (e.g., via ajax or reading from a file), but once you or a library you're using has parsed it, it's not JSON anymore.
Only because object literals and JSON look similar, it does not mean that you can name them interchangeably. See also There's no such thing as a "JSON Object".
JSON has a much more limited syntax including:
"
and not '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With