JSON
WHAT IS JSON ??
JSON stands for JavaScript Object Notation.
JSON is a text format for storing and transporting data.
It is commonly used for transmitting data in web applications.
JSON is “self-describing” and easy to understand.
JSON Example –
This example is a JSON string
{“name” : “Abdul”, “age”:30,”car”:null}
It defines an object with 3 properties:
name
age
car
Each property has a value.
JSON Data Types
In JSON, values must be one of the following
data types
a string
a number
an object (JSON object)
an array
a boolean
null
JSON values cannot be one of the following
data types
a function
a date
undefined
Key &Value –
The two primary parts that makeup JSON are
keys & values
Key: A key is always a string enclosed in
quotation marks.
Value: A value can be a string number,
boolean expression, array or object.
{“name”:”Abdul”}
The Key is “name” and the value is “Abdul”.
JSON Parse –
A common use of JSON is to exchange data
to/from a web server.
When receiving data from a web server, the
data is always a string.
Imagine we received this text from a web server
‘{“name”; “Abdul”, “age”:30, “city”: “New York”}’
Use the JavaScript function JSON.parse() to
convert text into a JavaScript object:
const obj = JSON. parse( ‘{” name”: “John”, “age” : 30, “city”: “New York”}’) ;
JSON.stringify()-
When sending data to a web server, the data has to be a string.
Convert a JavaScript object into a string with JsON.stringify().
Imagine we have this object in JavaScript
const obj = {name: “John”, age: 30, city: “New York”};
Use the JavaScript function JSON.stringify0 to
convert it into a string:
const myJSON JSON. stringify(obj);