JSON is a syntax for storing and exchanging data.
JSON is an easier-to-use alternative to XML.
JSON Syntax Rules:
Data is in name/value pairs
Data is separated by commas
Curly braces hold objects(note: one xml record can be treated as one json object)
Square brackets hold arrays
observer below examples to understand how to create JSON documents.
ex1(one object with one data field):
xml:
<firstName>john</firstName>
json:
{ "firstName": "john" }
ex2(one object with two data field):
xml:
<firstName>john</firstName>
<lastName>Doe</lastName>
json:
{"firstName":"John", "lastName":"Doe"}
ex3(one object with three data field):
xml:
<firstName>john</firstName>
<lastName>Doe</lastName>
<middleName>xyz</middleName>
json:
{"firstName":"John", "lastName":"Doe", "middleName":"xyz"}
ex4:
xml:
<employee>
<firstName>john</firstName>
<lastName>Doe</lastName>
</employee>
json:
{"employee": {"firstName": "john","lastName": "Doe"}}
ex5(One json object with nested array json objects):
xml:
<employees>
<emp>
<firstName>john</firstName>
<lastName>Doe</lastName>
</emp>
<emp>
<firstName>Anna</firstName>
<lastName>Smith</lastName>
</emp>
<emp>
<firstName>Peter</firstName>
<lastName>Jones</lastName>
</emp>
</employees>
json:
{
"employees": {
"emp":
[
{"firstName": "john","lastName": "Doe"},
{"firstName": "Anna","lastName": "Smith"},
{"firstName": "Peter","lastName": "Jones"}
]
}
}
ex6(One json object with nested multiple(same and different types) json objects):
xml:
<school>
<student>
<sno>101</sno>
<sName>sai</sName>
</student>
<student>
<sno>101</sno>
<sName>sai</sName>
</student>
<class>
<std>6</std>
<section>A</section>
</class>
</school>
json:
{
"school": {
"student": [
{"sno": "101","sName": "sai"},
{"sno": "101","sName": "sai"}
],
"class": {"std": "6","section": "A"}
}
}
online tool:
http://www.utilities-online.
use this tool to validate and understand how json is working.
XML vs JSON:
NOTE: XML and JSON both are the data interchange formats accessed over WEB. Both of these formats have their own pros/cons. No one is replacement of other. Use the right tool for the right job.
1. JSON format is lightweight over XML.
2. JSON can contain integers, strings, lists, arrays. XML is just elements and nodes that need to be parsed into integers and so on before it can be consumed.
3. The most important disadvantage of JSON is that the format is very hard to read for humans, and that, of course, every single comma, quote, and bracket should be in exactly the correct place. While this is also true of XML, JSON’s welter of complicated-looking syntax, like the }}]} at the end of the data snippet, may frighten the newbies and make for complicated debugging.
4. Serialization format for your data, JSON is smaller, lighterweight and generally faster than XML.
5. JSON is best for consumption of data in web applications from webservices for its size and ease of use, especially due to the built-in support in JavaScript.
6. For configurations file XML is better choice to make because it more human readable.
7. XML is document-oriented. JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.
8. XML and JSON both use Unicode.That help in support for internationalization.
9. JSON does not have afeature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data.
10. XML documents can contain any imaginable data type – from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java applets or ActiveX components.
11.JSON is a better data exchange format. XML is a better document exchange format. Use the right tool for the right job.
12. For Data delivery between servers and browsers, JSON is better choice.For storing Information in configuration files on the server side, XML is better choice.
13. Querying data: Using XPath, it’s possible to get direct access to a part of multiple parts of an XML data structure; no such interface exists for JSON. To get data from a JSON structure, you must know exactly where it is or else iterate over everything until you find it.
14. To extract data from database; XML is the only choice.
Imagine the computation overhead for parsing an xml fragment compared to the instant lookup in JSON.
No comments:
Post a Comment