FALLSEM2020-21 CSE3002 ETH VL2020210106410 Reference Material I 26-Sep-2020 6.5 JSON
FALLSEM2020-21 CSE3002 ETH VL2020210106410 Reference Material I 26-Sep-2020 6.5 JSON
Introduction
• JSON is a light-weight alternative to XML for data-interchange
• JSON = JavaScript Object Notation
• JSON is a syntax for storing and exchanging data.
• JSON is text, written with JavaScript object notation.
• It’s really language independent
• most programming languages can easily read it and instantiate objects or
some other data structure
Why use JSON?
• Since the JSON format is text only, it can easily be sent to and from a
server, and used as a data format by any programming language.
• JSON.parse()
Uses of JSON
• It is used while writing JavaScript based applications that includes
browser extensions and websites.
• JSON format is used for serializing and transmitting structured data
over network connection.
• It is primarily used to transmit data between a server and web
applications.
• Web services and APIs use JSON format to provide public data.
• It can be used with modern programming languages.
Features of JSON
• Easy to use - JSON API offers high-level facade, which helps you to simplify
commonly used use-cases.
• Performance - JSON is quite fast as it consumes very less memory space,
which is especially suitable for large object graphs or systems.
• Free tool - JSON library is open source and free to use.
• Doesn't require to create mapping - Jackson API provides default mapping
for many objects to be serialized.
• Clean JSON - Creates clean, and compatible JSON result that is easy to
read.
• Dependency - JSON library does not require any other library for
processing.
Exchanging Data
• When exchanging data between a browser and a server, the data can
only be text.
• JSON is text, and we can convert any JavaScript object into JSON, and
send JSON to the server.
• We can also convert any JSON received from the server into JavaScript
objects.
• This way we can work with the data as JavaScript objects, with no
complicated parsing and translations.
Sending Data
• If you have data stored in a JavaScript object, you can convert the object into JSON, and
send it to a server:
<!DOCTYPE html>
<html>
<body>
<h2>Convert a JavaScript object into a JSON string, and send it to the server</h2>
<script>
var myObj = { name: "John", age: 31, city: "New York" };
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;
</script>
</body>
</html>
Receiving Data
• If you receive data in JSON format, you can convert it into a JavaScript
object:
• In JavaScript, you can write string values with double or single quotes:
JavaScript
• { name:'John' }
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>
Why JSON is Better Than XML?