Json
Json
The JSON syntax is derived from JavaScript object notation, but the JSON format is text only.
Code for reading and generating JSON exists in many programming languages.
The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a
JavaScript program can easily convert JSON data into JavaScript objects.
Since the format is text only, JSON data can easily be sent between computers, and used by any
programming language.
JavaScript has a built in function for converting JSON strings into JavaScript objects:
JSON.parse()
JavaScript also has a built in function for converting an object into a JSON string:
JSON.stringify()
Storing Data
When storing data, the data has to be a certain format, and regardless of where you choose to store
it, text is always one of the legal formats.
JSON Syntax
Example
"name":"John"
JSON
{"name":"John"}
JavaScript
{name:"John"}
JSON Values
a string
a number
an object
an array
a boolean
null
In JavaScript values can be all of the above, plus any other valid JavaScript expression, including:
a function
a date
undefined
JSON
{"name":"John"}
In JavaScript, you can write string values with double or single quotes:
JavaScript
{name:'John'}
JavaScript Objects
Because JSON syntax is derived from JavaScript object notation, very little extra software is needed
to work with JSON within JavaScript.
With JavaScript you can create an object and assign data to it, like this:
Example
Example
// returns John
person.name;
Example
// returns John
person["name"];
Example
person.name = "Gilbert";
Example
person["name"] = "Gilbert";
JSON vs XML
Both JSON and XML can be used to receive data from a web server.
The following JSON and XML examples both define an employees object, with an array of 3
employees:
JSON Example
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
XML Example
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
Both JSON and XML can be parsed and used by lots of programming languages
JSON is shorter
XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.
Using XML
Using JSON
JSON is used when writing javascript based application which incudes browser extension and
websites.
JSON, data exchange format is used for serializing and transmitting structured data above
network connection.
This is mainly used to transmit data between server and web application.
Web services and APIs use JSON format to offer public data.
JSON can be used with modern programming languages.
JSON Data Types
a string
a number
an array
a boolean
null
a function
a date
undefined
JSON Strings
Example
{"name":"John"}
JSON Numbers
Example
{"age":30}
JSON Objects
Example
{
"employee":{"name":"John", "age":30, "city":"New York"}
}
JSON Arrays
{
"employees":["John", "Anna", "Peter"]
}
JSON Booleans
Example
{"sale":true}
JSON null
Example
{"middlename":null}
JSON.parse()
When receiving data from a web server, the data is always a string.
Parse the data with JSON.parse(), and the data becomes a JavaScript object.
Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
Make sure the text is in JSON format, or else you will get a syntax error.
Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = obj.name;
</script>
Array as JSON
When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript
array, instead of a JavaScript object.
Example
const text = '["Ford", "BMW", "Audi", "Fiat"]';
const myArr = JSON.parse(text);
JavaScript Objects
Example
Example
Example
You can also access object values by using bracket ([]) notation:
Example
Looping an Object
Example
In a for-in loop, use the bracket notation to access the property values:
Example
In JSON, array values must be of type string, number, object, array, boolean or null.
In JavaScript, array values can be all of the above, plus any other valid JavaScript expression,
including functions, dates, and undefined.
JavaScript Arrays
Example
Example
Example
myArray[0];
Arrays in Objects
Example
{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}
Example
myObj.cars[0];
Example
JSON Parser
The eval() function can compile and execute any javascript. This represent a potential security
problem.
It is safer to use a JSON parser to convert a JSON text to a javascript object. A JSON parser identifies
only JSON text and does not compile scripts.
In browsers that provide native JSON hold, JSON parsers are faster.
Native JSON support is integrated in newer browsers and in the newest ECMAScript (javascript)
standard.
HttpEntity he=httpResponse.getEntity();
InputStream is=he.getContent();
If we get the HttpResponse as XML format, then we require to parse it. The following fragment is
used to parse the XML.
HttpEntity he=httpResponse.getEntity();
String xml=EntityUtils.toString(he);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newinstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(is);
NodeList nl=doc.getElementsByTagName();
for(i=0;i<nl.getLength();i++)
Element e=nl.item(i);