What is JSON
- JSON stands for JavaScript Object Notation.
- JSON is lightweight data-interchange format.
- JSON is easy to read and write than XML.
- JSON is language independent.
- JSON supports array, object, string, number and values.
JSON Example
In this tutorial, you will get a lot of JSON examples to understand the topic well. The JSON file must be save with .json extension. Let's see a simple JSON example.File: Employee.json
{"employees":[
{"name":"Mukesh", "email":"mukesh14j@gmail.com"},
{"name":"Raj", "email":"raj@gmail.com"},
{"name":"James", "email":"james@gmail.com"}
]}
.
Features of JSON
- Simplicity
- Openness
- Self Describing
- Internationalization
- Extensibility
- Interoperability
JSON vs XML
A list of differences between JSON and XML are given below.| No. | JSON | XML |
|---|---|---|
| 1) | JSON stands for JavaScript Object Notation. | XML stands for eXtensible Markup Language. |
| 2) | JSON is simple to read and write. | XML is less simple than JSON. |
| 3) | JSON is easy to learn. | XML is less easy than JSON. |
| 4) | JSON is data-oriented. | XML is document-oriented. |
| 5) | JSON doesn't provide display capabilities. | XML provides the capability to display data because it is a markup language. |
| 6) | JSON supports array. | XML doesn't support array. |
| 7) | JSON is less secured than XML. | XML is more secured. |
| 8) | JSON files are more human readable than XML. | XML files are less human readable. |
| 9) | JSON supports only text and number data type. | XML support many data types such as text, number, images, charts, graphs etc. Moreover, XML offeres options for transferring the format or structure of the data with actual data. |
JSON Example
- {"employees":[
- {"name":"Vimal", "email":"vjaiswal1987@gmail.com"},
- {"name":"Rahul", "email":"rahul12@gmail.com"},
- {"name":"Jai", "email":"jai87@gmail.com"}
- ]}
XML Example
<employees>
<employee>
<name>Vikas</name>
<email>vikaskumar@gmail.com</email>
</employee>
<employee>
<name>Kumar</name>
<email>kumar@gmail.com</email>
</employee>
<employee>
<name>rahul</name>
<email>rahul44@gmail.com</email>
</employee>
</employees>
Similarities between JSON and XML
- Both are simple and open.
- Both supports unicode. So internationalization is supported by JSON and XML both.
- Both represents self describing data.
- Both are interoperable or language-independent.
JSON Example
JSON can be created by object and array. Each object can have different data such as text, number, boolean etc. Let's see different JSON examples using object and array.JSON Object Example
A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.The { (curly brace) represents the JSON object.
{
"employee": {
"name": "Mukesh",
"age": 27,
"working": true
}
}
JSON Array example
The [ (square bracket) represents the JSON array. A JSON array can have values and objects.Let's see the example of JSON array having values.
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
Let's see the example of JSON array having objects.
[
{"name":"James", "email":"james@gmail.com"},
{"name":"Bond", "email":"bond@gmail.com"}
]
JSON Example 1
- {"employees":[
- {"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
- {"name":"Bob", "email":"bob32@gmail.com"},
- {"name":"Jai", "email":"jai87@gmail.com"}
- ]}
<employees>
<employee>
<name>Shyam</name>
<email>shyamjaiswal@gmail.com</email>
</employee>
<employee>
<name>Bob</name>
<email>bob32@gmail.com</email>
</employee>
<employee>
<name>Jai</name>
<email>jai87@gmail.com</email>
</employee>
</employees>
JSON Examples
{"menu": {"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Save", "onclick": "SaveDoc()"}
]
}
}}
The XML representation of above JSON example is given below.
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateDoc()" />
<menuitem value="Open" onclick="OpenDoc()" />
<menuitem value="Save" onclick="SaveDoc()" />
</popup>
</menu>
JSON Object
JSON object holds key/value pair. Each key is represented as a string in JSON and value can be of any type. The keys and values are separated by colon. Each key/value pair is separated by comma.
The curly brace { represents JSON object.
Let's see an example of JSON object.
{
"employee": {
"name": "kumar",
"age": 27,
"working": true
}
}
In the above example, employee is an object in which "name", "age" and "working" are the key. In this example, there are string, number and boolean value for the keys.
JSON Object with Strings
The string value must be enclosed within double quote.
{
"name": "mukesh",
"email": "mukesh14j@gmail.com"
}
JSON Object with Numbers
JSON supports numbers in double precision floating-point format. The number can be digits (0-9), fractions (.33, .532 etc) and exponents (e, e+, e-,E, E+, E-).
{
"integer": 34,
"fraction": .2145,
"exponent": 6.61789e+0
}
JSON Object with Booleans
JSON also supports boolean values true or false.
{
"first": true,
"second": false
}
JSON Nested Object Example
A JSON object can have another object also. Let's see a simple example of JSON object having another object.
{
"firstName": "Mukesh",
"lastName": "Kumar",
"age": 27,
"address" : {
"streetAddress": "10 Mortensen Avenue, Salinas, CA",
"city": "New Jersey",
"state": "US",
"postalCode": "12001"
}
}
JSON Array
JSON array represents ordered list of values. JSON array can store multiple values. It can store string, number, boolean or object in JSON array.
In JSON array, values must be separated by comma.
The [ (square bracket) represents JSON array.
JSON Array of Strings
Let's see an example of JSON arrays storing string values.
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
JSON Array of Numbers
Let's see an example of JSON arrays storing number values.
[12, 34, 56, 43, 95]
JSON Array of Booleans
Let's see an example of JSON arrays storing boolean values.
[true, true, false, false, true]
JSON Array of Objects
Let's see a simple JSON array example having 4 objects.
{"employees":[
{"name":"vivek", "email":"vivek@gmail.com", "age":10},
{"name":"kumar", "email":"kumar@gmail.com", "age":39},
{"name":"James", "email":"james@gmail.com", "age":38},
{"name":"Bond", "email":"bond@gmail.com", "age":67}
]}
JSON Multidimensional Array
We can store array inside JSON array, it is known as array of arrays or multidimensional array.
[
[ "x", "y", "z" ],
[ "u", "s", "r" ],
[ "v", "w", "t" ]
]
Read More
Java 7 featuresHow to Reset Arraylist In Java
How HashMap Work in Java
Why wait (), notify () and notifyAll () must be called from synchronized block or method in Java
XPath to locate Information in XML
Internals of Garbage Collector
Reference Type in Java
Different Ways to Create ObjectClass Loaders in Java
Producer Consumer Problem
Why String is Final in Java
Singleton Class using Enum
Exceptional Handling in Java
No comments:
Post a Comment