Understanding Basic JSON

DZone 's Guide to

Understanding Basic JSON

Eat your heart out, XML.

· Database Zone ·
Free Resource

JSON (JavaScript Object Notation) is a data representation format that can be considered an XML competitor. It’s structure is totally different from the XML one (since the structural principles differ), but the general idea is the same – it should ensure easy data transportation between applications and services.

It is already implemented in several existing web services (small and large, like Twitter) and its share on the data exchange format market is growing due to its simplicity and organization.

You may also like: Core JSON: The Fat-Free Alternative to XML [DZone Refcard]

JSON Basics

Here is an example: Let’s suppose I have a data set, describing various computer parts, and I need to provide this data to an application. If I am going to think very abstract, there is a tree like structure, representing the base element (Computer Parts) and multiple child branches (for example: mainboard, keyboard, CPU, fan etc.)

By using the JSON data format, I can show the same tree-like structure just like this:

{ “parts” : {“part” : [{ “name” : “Mainboard” },{ “name” : “Keyboard”  },{ “name” : “CPU” },{ “name” : “FAN” }] }}

What I did here is created the base object – parts, and then initialized an array of part objects. Each part object has a name child object, that has a specific value set for it. Note that the array of objects is defined by square braces: [ and ].

Let’s suppose I want to add another object that would represent the price for an existing part. To do this, I simply put a comma after the existing value, declare the name for the new object and a value for it. So, my re-formatted JSON will look like this:

{ “parts” : {“part” : [{ “name” : “Mainboard”, “price” : “100” },{ “name” : “Keyboard” , “price” : “10” },{ “name” : “CPU”, “price” : “90” },{ “name” : “FAN”, “price” : “3” }] }}

Notice the persistent object : value structure. Now, I want to add another element to the existing part, that would store the name of the manufacturer. However, I do not want to set it as an additional simple object, but rather as an object added to the existing part object that will contain several other objects, so that I can store various details about the existing manufacturer. To do this, I am repeating the existing structure:

{ "parts" : {"part" : [{ "name" : "Mainboard", "price" : "100", "manufacturer" : {"name" : "MAN1", "location" : "USA"} },{ "name" : "Keyboard" , "price" : "10", "manufacturer" : {"name" : "MAN2", "location" : "USA"}},{ "name" : "CPU", "price" : "90", "manufacturer" : {"name" : "MAN3", "location" : "USA"}},{ "name" : "FAN", "price" : "3","manufacturer" : {"name" : "MAN1", "location" : "USA"} }] }}

However, as a part of the existing array element I am introducing an object, that has a value that is a set of attributes (other objects with values):

"manufacturer" : {"name" : "MAN1", "location" : "USA"}

This structure can be continued inside the next branch and so on. I can also introduce multiple arrays instead of elements, if additional object nesting is required.

JSON Data Manipulation Tools

These are just the very basics of JSON and hopefully you understood the fundamental document formatting methods. There are some great tools I could recommend looking at when it comes to JSON data manipulation.

First of all, there is the JSON Formatter tool that can help you fix errors in existing JSON documents. Then, when it comes to XML -> JSON and backwards conversion, I would highly recommend JSON Editor, also an online tool with some of the essential components to perform the conversion and structural check.

And although this is a neat tool, I would encourage you to work with JSON directly, even when converting from XML, since I know that many users can be tempted to switch to the tool instead of understanding existing JSON code. But it has a great potential for experimentation, so it can give you a head start in the right direction when you need to understand some JSON code.

Further Reading

Pain-Free JSON Formatting in Chrome

JSON With MariaDB Platform: What Is JSON and Why Use It?

data representation, javascript object notation, json, json data manipulation, json editor, json formatting, project mgmt., xml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}