JSON v0.1
JSON v0.1
• Introduction
• JSON Structure
• JSON Schema
• Add-ons
• Data Transmission
3 JSON | © SmartCliff | Internal | Version 1.0
Introduction
What? Why?
JSON
• JSON is a lightweight data-interchange format. i.e. it’s a text-based data exchange format that is used
• It is easy for humans to read/write and also it is easy for machines to parse and generate.
• It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition -
December 1999.
Why JSON?
• Provide support for all browsers and can be used with modern programming languages.
• JSON is often used as a common format to serialize and deserialize data in applications that
communicate with each other over the Internet.
• JSON is text which can be converted to any object of JavaScript into JSON and send this JSON to the
server.
Chronicle of JSON
• In December 2005, Yahoo! starts offering some of its web services in JSON.
Features of JSON
• Readability: The JSON data format is very easy to read since it is made up of data formats that have
• Size: The size of data in JSON is very less, tons of JSON data would add up to a few Megabytes of
• Independent: The JSON language is independent of data that is entered, it does not depend on other
languages to be implemented.
• Less Verbose: JSON has a more dense style when compared to languages like XML.
Features of JSON
• Clean JSON: Creates clean, and compatible JSON result that is easy to read.
• Faster: JSON makes use of fewer data resources which reduces the cost of the server load and
• Structured Data: JSON uses a map data structure to store data. The key or value pairs can restrict
your task, but you will get a predictable and easy-to-understand data model.
• Easy to use: JSON API offers high-level facade, which helps you to simplify commonly used use-
cases.
9 JSON | © SmartCliff | Internal | Version 1.0
JSON Structure
Introduction
Structure of JSON
• In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or
associative array.
Structure of JSON
Example
{
"languages":
- Objects
[
{
- Array
"name":"java",
"ver": 9.0
}, In this case, “Languages” array holds 2 objects.
{
"name":"python",
"ver": 3.0
}
]
}
Data Types
• Numbers - It includes real number, integer, floating number or an exponent of base 10.
• Boolean - The Boolean data type represents either True or False values
Data Types
• Null - The Null value denotes that the associated variable doesn’t have any value
• Object - It is a collection of key-value pairs and always separated by a comma and enclosed in curly
{
brackets. "id": 110,
o Example: "language": "Python",
"price": 1900,
}
• Array - It is an ordered sequence of values separated.
{
o Array should be used when the key names are sequential integers. "ids" : [
{"id" :
o Example: { 1},
"ids" : (OR) {"id" :
2},
["1","2","3"] {"id" : 3}
16 JSON | © SmartCliff | Internal | Version} 1.0
]
}
JSON Data types
{ ],
"trainees": “courses":
[ [
{ {
"firstName": "John", "name": "Java",
"lastName": "Smith", "id":1
"age": 21 },
}, {
{ "name": "JavaScript",
"firstName": "Jane", "id":2
"lastName": "Randy", }
"age": 20 ]
training.json
} }
Similarities
Differences
JSON XML
JSON has no display capabilities. XML provides a capability to display data because it
is a markup language.
JSON supports only UTF-8 encoding XML supports many encoding formats.
Challenges
• Though JSON brings some benefits, it can become a problem when consuming and integrating data
from different JSON services since developers need to be aware of the structure of the data.
• It becomes necessary to discover at least partially the underlying structure in order to properly process
the data.
• Solution: web developers must often interact with APIs publishing a set of JSON-based services.
• Problem–1: They need to understand and manage the JSON documents returned by those services.
• Problem–2: They need to compose several JSON-based services since their implicit structure can
differ.
23 JSON | © SmartCliff | Internal | Version 1.0
JSON Schema
Challenges
• It does not enforce data types, constraints, or relationships, which means that data can be inconsistent,
• This can lead to data quality issues and errors in data analysis and reporting.
• It can quickly become complex and difficult to manage, especially when dealing with nested structures.
Schema
Schema Definition
• $schema: specifies which draft of the JSON Schema standard the schema adheres to.
• $id: sets a URI for the schema. You can use this unique URI to refer to elements of the schema from
• title and description: state the intent of the schema. These keywords don’t add any constraints to
• type: defines the first constraint on the JSON data. In the product catalog example below, this
Example
• Validator:
• https://jsonlint.com/
• https://jsonformatter.curiousconcept.com/
• https://codebeautify.org/jsonvalidator
• Beautifier/Formatter:
• https://codebeautify.org/jsonviewer
• https://beautifytools.com/json-beautifier.php
• https://www.liquid-technologies.com/online-json-to-schema-converter
• https://www.liquid-technologies.com/online-json-schema-validator
29 JSON | © SmartCliff | Internal | Version 1.0
Add - Ons
JSONPath
• You use a JSONPath expression to traverse the path to an element in the JSON structure.
• You start at the root node or element, represented by $, and reach the required element in the JSON
structure to extract data from it.
• You can use either the dot-notation or the bracket-notation to form the expressions.
• Online Tools:
• JSON is a text-based data exchange format derived from JavaScript that is primarily used in web
services and other connected applications.
• While JSON looks like regular JavaScript, it's better to think of JSON as a data format.
• Though the syntax of JSON is similar to the JavaScript object, JSON is different from JavaScript
objects.
JSON can be created and used by other languages. JavaScript objects can only be used in JavaScript.
• Syntax: JSON.parse(text,[callback]);
Example - 1:
let json = '{"name": "Peter", "age": 22, "country": "United States"}';
let obj = JSON.parse(json);
console.log(obj);
• Avoid using functions in JSON, the functions will lose their scope, and you would have to use eval() to
convert them back into functions.
Example - 2:
const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
function reviver(key, value) {
if (typeof value === "string" && dateFormat.test(value)) {
return new Date(value);
}
return value;
}
const text = '{ "date": "2016-04-26T18:09:16Z" }';
const dateObj = JSON.parse(text, reviver);
console.log(typeof dateObj.date);
• While developing an application using JavaScript, many times it is needed to serialize/convert the data
to strings for storing the data in a JSON.
• This conversion of an object to a string can be easily done with the help of the ‘JSON.stringify()’
method.
Example - 1:
const jsonData = { name: "John", age: 22 };
const jsObj = JSON.stringify(jsonData);
console.log(jsObj);
Local Storage
• The web storage API provides mechanisms for the client’s browser to securely store and easily access
key-value pairs.
• This is useful for storing simple data such as usernames or emails, as well as checking whether a user
can access a particular resource by storing data such as access and refresh tokens.
• LocalStorage is a web storage mechanism that allows us to store data on the client’s browser that
persists even after the browser window is closed.
• ‘localStorage’ object stores the data with no expiration date until the browser cache is cleared.
// Create an object
const userData = {
job: "Programmer",
skills: [ { id: 4200, name: "Angular" }, { id: 3000, name: "React" }, { id: 8080, name: "Vue" }, ],
};
// Store the object into storage
localStorage.setItem("userData", JSON.stringify(userData));