Introduction to JSON and javascript objects

What is JSON?

JSON is the JavaScript Object Notation, primarily used to transmit data between the server and the application. This acts as an alternative to XML, being much lighter and readable. JSON is language independent and is easily parse-able in almost all available programming languages. JSON is the standard format that describes how ordered lists and unordered maps, strings booleans and numbers can be represented in a string.

Something like the following represents a javascript object:

Now when I stringify the above as:

It gives me nothing but the JSON text as:

JSON.stringify() turns an object into a JSON text and stores that JSON text in a string.

Note that, the key and values in JSON must always be enclosed within double quotes.

JSON supports the following data types:

  1. Number
  2. String
  3. Boolean
  4. Array
  5. Object
  6. null

To demonstrate each of them, check the below snapshot:


The complete data that you see above is a JSON text.

To convert a JSON text into an object, we can use the eval() function which is very fast and parse the JSON text to a corresponding object structure. However, using eval poses a security threat unless and until the JSON text that is to be parsed is trusted.

As an alternative to eval(), we must prefer using the JSON parser instead.

There are browsers that provide native JSON support, where the JSON parsers prove to be much more faster than eval().

JSONSo, the Javascript Object is something type internal (native) to Javascript, similar to what classes mean to in Java. JSON just provides us a standard format for data transfer that participating languages can understand, very similar to what XML does. We cannot pass a Javascript Object directly to Java or PHP, because each language represent their object differently and so does Javascript. They need a standard common JSON notation, which almost all the languages can understand, with their respective parsing implementations .

Let us all discuss our all the clarifications you need on this.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.