What is JSON?
Bhartee Rameshwar Sahare
Posted on March 12, 2023
Javascript object notation (JSON) is a standard text-based data format used in web development to send and receive data.
API is an application programming interface designed for lightweight data interchange (text-based data exchange format) between two computer applications operating on the same hardware device or between different computers in different geographical areas.
The format is to transfer data from client to server and server to client.
Example:
I am login with the Facebook application (client side), when I click my profile page, that time the application sends a request to the Facebook server(server side) and gives me the response in a format like JSON.
Advantage:
- It is supported by all programming languages.
- It can be used on all platforms.
- It is a lightweight database standard, so data transmission is fast.
Where JSON Is Used:
- Web Application
- API's
- Mobile application
- Gaming Api's
- IOT
Data Type Supported:
- String
- Number
- Array
- Null
- Boolean
- Object
JSON Syntax and Datatype:
- Every piece of JSON data is enclosed in curly brackets.
- Key-value pairs are used to describe JSON data.
- Always surround the word "key" in double quotes.
- Keys and values must always be separated by a comma (:).
- Values should be expressed correctly; double quotes for Strings are
- preferred over Boolean quotes for Numbers.
- Commas (,) should be used to divide text.
- Curly braces should be used for objects and collection square brackets.
[
{
"id": 1,
"name": "test",
"email": "test@gmail.com",
"status": "active",
"class": {
"id": 1,
"name": "1st",
"sub_class": {
"id": 1,
"name": "A"
}
}
},
{
"id": 1,
"name": "test",
"email": "test@gmail.com",
"status": "active",
"class": {
"id": 1,
"name": "1st",
"sub_class": {
"id": 1,
"name": "A"
}
}
}
]
JSON Array Declaration:
[
"computer_science", "mechanical_engineer", "electrical_engineer"
]
JSON Object Declaration:
{
"id": 1,
"name": "test",
"class": "1st",
"dob": "1970-02-15"
"certicate_achieve": ["rest_api", "json", "ruby"]
}
Converting JSON Text to a JavaScript Object:
- JSON.parse(): Take the JSON string and convert it into a javascript object.
- JSON.stringify(): convert JavaScript object(JSON) into JSON string(useful while sending over the network)
// JSON.stringify()
obj = {"id":1, "name": "test"}
{id: 1, name: 'test'}
JSON.stringify(obj)
'{"id":1,"name":"test"}'
// JSON.parse()
data = JSON.stringify(obj)
'{"id":1,"name":"test"}'
JSON.parse(data)
{id: 1, name: 'test'}
Posted on March 12, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.