json

JSON Basics

In this article, we will learn about :

– what is JSON?
– syntax for creating JSON
– JSON values
– JSON validators

 

What is JSON

 
json
 
JSON stands for Java Script Object Notation.

It is a data interchange format like XML, but lightweight compared to xml.

Douglas Crockford is the creator of JSON.

JSON is language independent.

 

JSON Syntax

 
JSON can be written in following 2 ways :

1) As a collection of name:value pairs
2) As an ordered list of objects

 

Collection of key-value pairs

 
This is the way objects are represented in JSON.

It can be written with braces { } and individual key:value pairs separated using comma.

The key and value are separated by the colon operator(:)

Here are some examples:
 

JSON object as an empty collection


{}; 

 

JSON Object with single name-value pair


{"Name" : "Ryan"};

 

JSON object with two members


{
"firstName":"John",
"lastName":"Doe"
}; 

 

Ordered list of objects

 
This is the way arrays are represented in JSON.

Ordered lists are added within opening ([) and closing (]) square brackets.

Here are some examples :
 

Empty JSON Array


[];

 

JSON Array of primitive values


[1,true,null];

 

Nested JSON Array of objects


[
{"firstName":"John","lastName":"Doe"},
{"firstName":"Lisa","lastName":"Dixon"}
];

 

JSON values

 
A JSON value can only be a string, number, object, array, boolean or null.
 
We have already seen objects and arrays.
 
JSON strings are added within double quotes.(e.g. “Hello”)

Boolean values are written as lowercase true or false.

Null is represented as lowercase null.

Control character must be escaped using \ character.
 

JSON Validators

 
Your IDE may already be supporting JSON validation. You may also download some plugins for same.

For example, if you are using Eclipse, you can download JSONEditor plugin from Eclipse marketplace.
 
Also, here are some online validators you can use for creating and validating JSONs.

http://jsoneditoronline.org/

https://jsonformatter.curiousconcept.com/

http://www.cleancss.com/json-editor/

http://jsonlint.com/

 

JSON Document and Media type

The file type for JSON files is “.json”

The MIME type for JSON text is “application/json”

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags