How to write code for JSON? (Class 24)

How to write code for JSON

How to write code for JSON?

In the previous class, we learned the JSON concept in detail. Now, it’s time to know how to write code for JSON. For this purpose, we will learn different Use Cases of JSON to fully understand. Before moving to our main topic, in the first instance, we discuss Client-Server communication.

 

Client Server Communication

We use JSON for data transmission. The client is about a platform that is being used like a Mobile app, website, desktop app, etc. For example, we use the Facebook mobile app and it is a client. When we log in on Facebook, a request goes to the Server which acts as an API, interacts with the database, and a response is received from there. This process takes milliseconds and responses are given back to users in seconds. Our login request travels in JSON form to the server and then it communicates with the database and returns to us through a server in JSON form. We have libraries and multiple built-in classes on the client side to read JSON structure.

 

Is JSON a file or code?

JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays.

Responsibilities of backend and front-end developers

It is the responsibility of the backend developer to make JSON. The concerned developer should try to keep it simple for easy understanding because a complex structure is considered difficult to decode. The simple structure also represents good logic. JSON parsing/ decoding is the responsibility of the front-end developer.

 

Platform to make JSON code

We can write JSON code on online platforms like jsoneditoronline having URL:

https://jsoneditoronline.org/

The benefit of using this website is that we instantly get errors in case of any mistake.

 

What does JSON code look like?

A simple JSON code in the form of an Array and  Object would be as under:-

 [

“Ali”,

45,

3.45,

false

]

 

{

“name”:”Ali”,

“roll_no”:45,

“gpa”:3.45,

“ic_check: false”

}

 

How to declare JSON data?

There can be different scenarios to declare JSON data called Use Cases of JSON. We will briefly discuss them here to understand them:

 

JSON Objects inside JSON Array

When we put JSON objects inside a JSON array, it is called nesting. If a front-end developer uses less nesting then it will be easy for a front-end developer to parse. In the current scenario, array is outside whereas the JSON object is placed inside.

Format:

[

{},

{},

{}

]

 

Real Example:

[

{

“name”:”Ali”,

“roll_no”:45,

“gpa”:3.45,

“ic_check: false”

},

{

“name”:”Usman”,

“roll_no”:35

“gpa”:3.45,

“ic_check: true”

}

]

 

JSON Array inside JSON Object

We can also put JSON Array inside a JSON Object as given hereunder:

{

“name”:”Ali”,

“roll_no”:45,

“gpa”: 3.45,

“subjects”: [“physics”,”chemistry”, “Computer Science”, “Mathematics”]

}

 

Nested JSON Objects

There can also be nested JSON objects like:

{

“name”:”Ali”,

“roll_no”:45,

“gpa”: 3.45,

“subjects”: { “sub_1” : ”Computer Science”, “sub_2” : ”Mathematics”, “sub_3”: “Physics” }

}

 

Nested JSON Array:

Though we can make nested JSON Array it is unnecessary and may not be made. As described earlier, we have to keep the JSON as simple as possible as under:-

[

“Ali”,

“Hamza”,

“Usama”,

“Umar”,

“Ayesha”,

“Fatima”,

“Fozia”,

“Mehwish”

]

 

Nested JSON Objects & JSON Array

We can make nested JSON objects with JSON Array as under:-

{

“name”:”Ali”,

“roll_no”:45,

“gpa”: 3.45,

“subjects”: [

{“subject_title”:”Physics”, ”subject_code”: 002, “subject_author”:”Muhammad Ali”},

{“subject_title”:”Chem”, ”subject_code”: 003, “subject_author”:”Muhammad Ali”},

{“subject_title”:”CS”, ”subject_code”: 004, “subject_author”:”Muhammad Ali”},

{“subject_title”:”Math”, ”subject_code”: 006, “subject_author”:”Muhammad Ali”},

]

}

 

Encode all University Data into JSON structure:

For assignment purposes, you can make a university data JSON. For example:-

{

“program_title” : “Comsats”,

“program_manager” : “Ijaz Farooq”,

“head_office”: “ASTP”,

“total_centers”: 45,

“total_earning”: 8100000000,

“total_courses”: 9,

“total_graduates”: 56000

“pu_lahore”: {

“center_location”:”AABX, 004 ABX”

“center_manager”:”XYZ”,

“center_sne”:”ABC”,

“total_domains”:4,

“total_trainer”:4,

“total_graduates”:1500,

“mobile_app_dev”: {

“course_title”: “Mobile App Development”,

“trainer_name”: “ABC”,

“course_duration”: 3,

“class_time”: “9:30”,

“total_students”: 140,

“students”: [

{

“name”:”Zari Abid”,

“rol_no”:001,

“email”:”abx@xyz.com”,

“nic”: 23232342342,

“phone”:9231245542,

“hobbies”: [“cricket”,”gardening ”]

},

{

“name”:”Qasim Javed”,

“rol_no”:001,

“email”:”abx@xyz.com”,

“nic”: 23232342342,

“phone”:9231245542

},

{

“name”:”Arifa Asif”,

“rol_no”:001,

“email”:”abx@xyz.com”,

“nic”: 23232342342,

“phone”:9231245542

},

{

“name”:”Tasadduf”,

“rol_no”:001,

“email”:”abx@xyz.com”,

“nic”: 23232342342,

“phone”:9231245542

},

}

},

“lcwu”: {

},

“uet_lahore”: {

},

}

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.