JSON
JSON
JSON is a subset of the JavaScript Programming language, Standard ECMA-262 3rd Edition-
December 1999 where both,
JSON is a lightweight text format that is independent of any programming Languages thus making it an
ideal option for data-interchange in today's Business world.
Advantages
Code for parsing and interchanging JSON data is readily available in almost all modern
programming languages.
Unlike Javascript, JSON can retrieve values from anywhere as it supports all languages.
Performs asynchronous data calls without requiring a page refresh. This is widely used
for asynchronous browser/server communication
APIs and web services use JSON to transfer real-time sensitive data.
JSON supports for sending, reading, and receiving texts available in all the real-world business.
JSON-RPC is a Remote Procedure call (RPC) protocol built on JSON, which allows system to send
multiple notifications to the server.
JSON vs XML
JSON almost replaced XML which was previously leveraged as the only data interchange format.
JSON transfers data faster than XML because XML uses tags to describe data which increases
their data size.
JSON uses typed Objects, which can be parsed by any standard JavaScript function. Whereas,
XML uses type-less strings, which must be parsed by XML parser (XPath) during run-time, which
makes it more complex.
Limitations
JSON cannot handle large data. (Need to leverage other formats, which you will learn later in
this course.)
JSON does not have a feature to support 'comments'. This could be included as an additional
attribute alone.
Datatypes
In JSON, values should strictly use one of the following data types:
In JSON syntax,
Keys must necessarily be strings, written with double quotes. Example: "Name"
Data can be a Name/value pairs that consist of a field name inside double quotes, followed by a
colon, and then by a value inside double quotes Example:"Name":"Peter"
Data must be separated by Commas**(,)**. Example: "Ram", "Peter"
Curly brackets {} to hold objects Example: { "Name":"Joseph" }
Square brackets [] hold arrays Example: {"employees":[ "Ram", "Peter", "Joseph" ]}
module.exports= function(){
json.forEach(function(element,index){
element['aggregate'] = parseInt(element['aggregate']);
per['sub1'] = parseInt(per['sub1']);
per['sub2'] = parseInt(per['sub2']);
per['sub3'] = parseInt(per['sub3']);
});
return json;
Example 1
<p id="Sample"></p>
<script>
var myObj, x;
x = myObj["Company"];
document.getElementById("Sample").innerHTML = x;
</script>
OR
Example 2
x = myObj.Company;
Looping an Object
for-in loop can be used to loop in object properties.
Example1:
<p id="sample"></p>
<script>
var myObj, x;
for (x in myObj) {
document.getElementById("sample").innerHTML += x + "<br>";
}</script>
Example2:
for (x in myObj) {
Nested Objects
Values in a JSON object could also be another JSON object.
Example
<p id="Sample"></p>
<script>
var myObj = {
"name":"Arun",
"age":30,
"cars": {
"car1":"Volkswagen",
"car2":"Hyundai",
"car3":"Fiat"
</script>
Nested JSON objects can be accessed by using the dot notation or bracket notation.
Example
x = myObj.cars.car2;
or:
x = myObj.cars["car2"];
Delete Objects
Properties:
Example
delete myObj.cars.car2
Accessing an Array
Example
<p id="Sample"></p>
<script>
var myObj, x;
myObj = {
"name":"Arun",
"age":29,
};
x = myObj.cars[0];
document.getElementById("Sample").innerHTML = x;
</script>
<p id="Sample"></p>
<script>
myObj = {
"name":"Arun",
"age":29,
};
for (i in myObj.cars) {
x += myObj.cars[i] + "<br>";
document.getElementById("Sample").innerHTML = x;
</script>
Nested Array
In Nested arrays, to access arrays inside arrays, use a for-in loop for each array.
Example
<p id="Sample"></p>
<script>
myObj = {
"name":"Arun",
"age":28,
"cars": [
for (i in myObj.cars) {
for (j in myObj.cars[i].models) {
x += myObj.cars[i].models[j] + "<br>";
}}
document.getElementById("Sample").innerHTML = x;
</script>
<p id="Sample"></p>
<script>
myObj = {
"name":"Arun",
"age":29,
"cars": ["Ford","BMW","Fiat"]
delete myObj.cars[1];
for (i in myObj.cars) {
x += myObj.cars[i] + "<br>";
document.getElementById("Sample").innerHTML = x;
</script>
JSON Parse
JSON.parse() function was developed as a safer alternative to eval.
While receiving data from the web server, the data is always in the string format, we have to parse
the data with JSON.parse() and the data becomes a JavaScript object.
Use JavaScript function JSON.parse() to convert this text into a JavaScript object:
<p id="sample"></p>
<script>
</script>
JSON Stringify
You should convert a JavaScript object into a string with JSON.stringify() function.
Example
<script>
document.getElementById("sample").innerHTML = myJSON;
</script>
There is a powerful npm package by the name json-rules-engine, which has rules built of simple JSON
structures and it is very light-weighted.
Apart from the regular JSON, we have other versions of JSON in use as well.
Google's GSON - Java library from Google to convert Java Objects into JSON and vice versa. In
addition, it paves room for simpler implementation by not requiring to annotate your classes.
Oracle’s JSONP is Java API for JSON processing. This consumes/produces streaming JSON text.
FasterXML’s Jackson - Can handle both JSON/non-JSON encodings. It is a set of data processing
tools powered with streaming JSON parser and generator library.
A type of injection attack that is injecting data into a web application to facilitate the execution or
interpretation of malicious data that takes advantage of any normal website by a third party with a
malicious script.
Avoid using Top-level arrays which are valid JavaScript that can be linked to a script tag.
Use HTTP POST instead of HTTP GET in JSON, because the GET request can be linked to any URL
with a script tag which is a web threat.
Use JSON.parse() instead of eval(), because eval() function will compile and execute any given
set of string, which can open your code during web attacks, where JSON.parse() only parses
JSON.