0% found this document useful (0 votes)
2 views

5. JSON

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5. JSON

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JavaScript Object Notation [JSON]

 Like XML, JSON also is a Platform Independent Language" which helps to store and transport
data.

 However, compared to XML, it’s a lightweight, easy for applications to parse and generate
by avoiding complicated parsing and translations

 JSON, as the name implies, which consists of data similar to "Object Notation” of JavaScript.
It's an extension of JavaScript.

 Hence if we receive data from a server in JSON format, we can directly use it like any other
JavaScript object.
 The filename extension of JSON is “.json“

 MIME type (Content Type) of JSON is "application/json“

Examples of Data Formats:

JAVA

String str1 = "EmpID=123 | EmpName=Deepak | EmpSal=200.12";

XML JSON
<employee> {
<emp-id>123</emp-id> "EmpID":123,
<emp-name>Deepak</emp-name> "EmpName":“Deepak",
<emp-salary>200.12</emp-salary> "EmpSal":200.12
</employee> }

JSON Syntax

 Data is in "name”: value pairs


 Data is separated by "commas “
 “Curly braces" hold objects {}
 "Square brackets" hold arrays []

JSON Data

 JSON data is written as name/value pairs.

 A name/value pair consists of a field name (Should be in double quotes) followed by a colon-
followed by a value

Ex: "employee-name “: “Deepak”

JSON value

 In JSON, values must be one of the following data types

1. String

2. Number

3. Boolean

4. NULL

5. An Object (JSON object)

6. An Array

7. An Object Array

In JSON,

 String values must be written with double quotes


 Numbers must be an integer/decimal values
 Boolean values must be true/false
 JSON NULL values must be null

Ex: { "name":“Deepak", "age":35, "isEmployed":true, "girlFriend":null }

JSON Array

 Values in JSON can be arrays

 JSON Arrays are surrounded by "Square Brackets [ ]“

 JSON Array values are separated by a commas

 Array values must be a valid JSON data type-(String, Number, Object, Array, Boolean or null)

Few examples for difference between XML & JSON


JSON /XML

JSON XML
JSON data has a data type, light weight XML data is type less
JSON types: string, number, array, Boolean, null,
Object All XML data should be string
Data is readily accessible as JSON objects XML data needs to be parsed.
JSON is supported by most browsers. Cross-browser XML parsing can be tricky
Retrieving value is easy Retrieving value is difficult
A fully automated way of Developers have to write JavaScript code to serialize/
deserialization/serialization JavaScript. de-serialize from XML.
It supports only UTF-8 encoding. It supports various encoding.
It doesn't support comments. It supports comments.
XML documents are relatively more difficult to read
JSON files are easy to read as compared to XML. and interpret.
It is less secured. It is more secure than JSON.

JSON Parser

 JAX-RS, JACSON, JERSEY is a Java API helps us to convert Java Object to JSON & vice-versa

 The Process of converting Java Object to JSON is called as "Marshalling" OR "Serialization “

 The Process of converting JSON to Java Object is called as "Unmarshalling" OR


"Deserialization”

You might also like