Skip to content

JSON

JavaScript Object Notation (JSON) is a lightweight data interchange format. It is based on JavaScripts object literal notation.

Although JSON is a subset of JavaScript, it is language independent. It can be used to exchange data between programs written in all modern programming languages. It is a text format, so it is readable by both humans and machines.  It is easy to implement and easy to use.

JavaScript Object Notation (JSON) is an open standard developed by Douglas Crockford, author of ,[amazon_link asins='0596517742|0596517742' template='SimpleLink2' store='threeninecons-21|threenine05-20' marketplace='UK|US' link_id='e5ba04ff-23c2-4cb0-a1db-90b3b9ad1b7c'] , that specifies a subset of the native JavaScript object-literal syntax for use in data representation, communication, and storage.

Prior to the JSON specification, most client-server communications were being delivered in much more verbose XML snippets. JavaScript developers often work with JSON web services and frequently define internal data using JSON syntax.

A JSON object is an unordered container of name/value pairs. A name can be any string.  A value can be any JSON value, including arrays and objects. JSON objects can be nested to any depth, but generally it is most effective to keep them relatively flat. Most languages have a feature that maps easily to JSON objects, such as an object, struct, record, dictionary, hash table, property list or associative array.

The JSON array is an ordered sequence of values. A value can be any JSON value, including arrays and objects.  A value can be any JSON value, including arrays and objects. Most languages have a feature that maps easily onto JSON arrays, such as arrays, vector, list or sequence.

Although JSON is language independent, I'll be using Node.js in my examples, however they should work within any JavaScript framework.

Creating a JSON Object

A JSON object in Node.js uses {} syntax to declare the JSON data type. In our initial example we'll create a simple JSON object to store details about a freelancer.

JS

We can extend this object to include a nested JSON object to include address information.

JS

Displaying Data

After we have created our JSON object we can print the contents out to console, by simply using console.log.  This will show a native JSON object in the console.

JS

Accessing Data

If you want to get a specific attribute of a JSON object, then you can call the attribute name directly.

JS

If you call an unknown attribute of a JSON object, you will get an undefined value.

You can iterate through your JSON object to retrieve what attributes have been defined.

JS

If you want to check whether a property has been defined on a JSO object you can use the hasOwnProperty function.  This is a boolean function so will return either true or false.

Editing Data

Editing JSON data can be performed by assigning new values to the object's attribute.

JS

You can also change a value by accessing the object like an array using the []

JS

JSON Array

We can also combine our object to include a collection of values.

JS

If you want to access a JSON array, for instance, using the details attribute, you can pass the index parameter to get a single JSON object.

JS
Gary Woodfine
Latest posts by Gary Woodfine (see all)