0% found this document useful (0 votes)
31 views7 pages

Understanding JSON: Definition and Uses

Uploaded by

vekariyaj37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

Understanding JSON: Definition and Uses

Uploaded by

vekariyaj37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit-3 JSON (JavaScript Object Notation) Web Designing-2

 3.1 Introduction of JSON:

 JSON stands for JavaScript Object Notation.


 JSON is a lightweight data-interchange format.
 JSON is plain text written in JavaScript object notation.
 The JSON syntax is derived from JavaScript object notation, but the JSON format is text only.
 JSON is easy to read and write than XML.
 JSON supports array, object, string, number and values.
 JSON is a text format for storing and transporting data.
 The JSON format was specified by Douglas Crockford.
 JSON is "self-describing" and easy to understand.
 The JSON filename extension is .json.
 Web services and APIs use JSON format to provide public data.
 It can be used with modern programming languages.
 Use of JSON:

 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.
 The JSON format is syntactically similar to the code for creating JavaScript objects.
 JavaScript program can easily convert JSON data into JavaScript objects.
 The JSON 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:
[Link]().
 JavaScript also has a built in function for converting an object into a JSON string:
[Link]().

 JSON Features:
 JSON is easy to read and write.
 It is a lightweight text-based interchange format.
 JSON is language independent.

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 1


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
 JSON Syntax:
JSON syntax is derived from JavaScript object notation syntax:
 Data is in name/value pairs
 Data is separated by commas
 Curly braces hold objects
 Square brackets hold arrays
Examples: {"name":"John"}

{"employees":[
{"name":"Sonoo", "email":"sonoojaiswal1987@[Link]"},
{"name":"Rahul", "email":"rahul32@[Link]"}
]}

 Similarities between the json and XML.


 Self-describing: Both json and xml are self-describing as both xml data and json data are
human-readable text.
 Hierarchical: Both json and xml support hierarchical structure. Here hierarchical means that the
values within values.
 Data interchange format: JSON and XML can be used as data interchange formats by many
different programming languages.
 Parse: Both the formats can be easily parsed.
 Retrieve: Both formats can be retrieved by using HTTP requests. The methods used for
retrieving the data are GET, PUT, POST.

 Different JSON vs XML

JSON XML
JSON stands for JavaScript object XML stands for an extensible markup
notation. language.
The extension of json file is .json. The extension of xml file is .xml.
The internet media type is The internet media type is
application/json. application/xml or text/xml.
It is extended from javascript. It is extended from SGML.
The data types supported by JSON are XML data is in a string format.
strings, numbers, Booleans, null, array.
It does not have any capacity to display XML is a markup language, so it has the
the data. capacity to display the content.

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 2


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
JSON has no tags. XML data is represented in tags, i.e., start
tag and end tag.
JSON is quicker to read and write. XML file takes time to read and write
because the learning curve is higher.
JSON can use arrays to represent the XML does not contain the concept of
data. arrays.
It can be easily parsed and little bit It is difficult to parse.
code is required to parse the data.
File size is smaller as compared to File size is larger.
XML.
It is less secure than XML. It is more secure than JSON.
Retrieving value is easy Retrieving value is difficult
It supports only UTF-8 encoding. It supports various encoding.

It doesn't support comments. It supports comments.


{ <?xml version="1.0" encoding="UTF-8"
"student": [ ?>
<root>
{ <student>
"id":"01", <id>01</id>
"name": "Tom", "lastname": <name>Tom</name>
"Price" <lastname>Price</lastname>
}, </student>
{ <student>
"id":"02", <id>02</id>
"name": "Nick", "lastname": <name>Nick</name>
"Thameson"
} <lastname>Thameson</lastname>
] </student>
</root>

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 3


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
 JSON objects (with string and numbers)

 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.
 JSON object literals are surrounded by curly braces {}.
 JSON cannot be an object. JSON is a string format.
 The data is only JSON when it is in a string format. When it is converted to a
JavaScript variable, it becomes a JavaScript object.
 Example: JSON Object

{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}

 JSON Object with Strings

 The string value must be enclosed within double quote.


 Example:
{
"name": "sonoo",
"email": "sonoojaiswal1987@[Link]"
}

 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-).
 Example:
{
"integer": 34,
"fraction": .2145, "exponent":
6.61789e+0
}

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 4


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
 JSON arrays and their Examples

 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.
 Arrays in JSON are almost the same as arrays in JavaScript.

 Array of strings:
example of JSON arrays storing string values.
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

 Array of Numbers:
example of JSON arrays storing number values. [12, 34,
56, 43, 95]

 Array of Booleans

example of JSON arrays storing Boolean values. [true,


true, false, false, true]

 Array of Objects:
JSON array example having 4 objects.
{"employees":[
{"name":"Ram", "email":"ram@[Link]", "age":23},
{"name":"Shyam", "email":"shyam23@[Link]", "age":28},
{"name":"John", "email":"john@[Link]", "age":33},
{"name":"Bob", "email":"bob32@[Link]", "age":41}
]}
OR
{
"name":"John", "age":30,
"cars":["Ford", "BMW", "Fiat"]
}

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 5


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
 MultiDimention Arrays:

 We can store array inside JSON array, it is known as array of arrays or


multidimensional array.
[
[ "a", "b", "c" ],
[ "m", "n", "o" ],
[ "x", "y", "z" ]
]

 JSON comments

 JSON doesn't support comments. It is not a standard. But you can do some tricks such as
adding extra attribute for comment in JSON object

{
"employee": { "name":
"Bob",
"salary": 56000,
"comments": "He is a nice man"
}
}
Here, "comments" attribute can be treated as comment.
 Convert JS Object into JSON String

<script>
var employees=[
{'name':'Ram','email': 'ram@[Link]','age' : 23},
{'name':'Shyam','email': 'shyam23@[Link]','age' : 28}
];
var s=[Link](employees);
[Link]("<h2>"+s+"</h2>");
</script>

Output:
[{"name":"Ram","email":"ram@[Link]","age":23},{"name":"Shyam","
email":"shyam23@[Link]","age":28}]

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 6


Unit-3 JSON (JavaScript Object Notation) Web Designing-2
 Convert JSON String into Plaintext

<script>
var s='[{"name":"Ram","email":"ram@[Link]","age":23},{"name":"Shyam",
"email":"shyam23@[Link]","age":28}]';
var obj=[Link](s);
for (var j = 0; j < [Link]; j++){
[Link]("<h2>"+obj[j].name+", "+obj[j].email+", "+obj[j].age+"</h2>");
}
</script>

Output:

Ram, ram@[Link], 23
Shyam, shyam23@[Link], 28

SHREE UTTAR GUJARAT BCA COLLEGE Page No. 7

Common questions

Powered by AI

JavaScript provides two built-in functions for JSON conversion. JSON data can be converted into JavaScript objects using the JSON.parse() function, which parses JSON strings into JavaScript objects. Conversely, JavaScript objects can be converted into JSON strings using the JSON.stringify() function . Example: var jsonString = '{"name":"John"}'; var obj = JSON.parse(jsonString); var newJsonString = JSON.stringify(obj).

JSON is preferred over XML in modern web development due to its simplicity and lightweight nature. It uses a syntax closely aligned with JavaScript, enhancing ease of use and interoperability in web applications where JavaScript is predominant . JSON's structure reduces overhead and increases transfer efficiency, which is crucial for developing fast and responsive web applications. Additionally, its straightforward parsing and object interchange formats align well with RESTful services commonly employed in modern web architectures .

JSON arrays are largely similar to JavaScript arrays, as they both use square brackets to hold ordered lists of values. However, JSON arrays are purely a data format used for serializing data, meaning they are static and lack methods associated with JavaScript arrays, such as push() or pop(). The role of JSON arrays is to convey structured data that can be easily converted into JavaScript arrays for programming manipulation .

Developers may face challenges when handling JSON data that requires comments or meta-information because JSON does not natively support comments . To include such information, developers often need to create workaround solutions, such as including a special 'comments' key within the JSON object to store this information, which can clutter the data structure and lead to inefficiencies. Additionally, these pseudo-comments must be correctly interpreted and managed by applications, increasing complexity .

JSON is generally quicker to read and write compared to XML, as it does not require tags and has a simpler syntax . Additionally, JSON is lightweight with a smaller file size than XML, making it efficient for transmitting data over networks . Furthermore, parsing JSON requires less code and effort compared to the more cumbersome parsing of XML .

JSON maintains human-readability by using a simple and concise syntax that resembles the structure of JavaScript objects, which includes name/value pairs, separated by commas, and organized within curly braces for objects and square brackets for arrays . Being a plain text format without complex markup, it remains easy for humans to read while ensuring ease of parsing for machines .

JSON ensures cross-language compatibility by keeping data representation as a text format without language-specific constructs, making it easy to parse and use across different programming languages . This is critical for web services and APIs that operate between diverse systems and languages, allowing seamless data interaction and transmission across platforms .

JSON mimics the hierarchical structure benefits of XML by allowing nested data representations within objects and arrays, enabling complex data relations and hierarchies to be expressed clearly . This hierarchical structuring is achieved using nested objects and arrays, making it possible to represent complex data contexts and relationships in a concise and understandable manner .

JSON is less secure than XML due to its lack of support for multiple encoding formats, limiting data encoding to UTF-8, which makes JSON files more susceptible to certain types of data breaches . Moreover, JSON does not support comments or metadata within its structure, which can be necessary in complex or sensitive data exchanges—the capabilities XML provides along with its more extensive security features .

JSON supports data serialization by transforming data structures or object states into a format that can be easily stored or transmitted and reconstructed later. This is achieved by using a simple text-based format where data is represented in key/value pairs and arrays, which makes it ideal for web applications needing to exchange data between servers and clients over networks . The lightweight nature of JSON minimizes the amount of data transmitted, speeding up data exchange and improving application performance .

You might also like