UNDERSTANDING THE JSON FORMAT

 On this page, you will learn about the structures of JSON. you will also learn different forms of storing data in JSON.


The JSON format is text-based and is independent of programming languages, meaning any of them can use it. It’s a great way to exchange data between different programs because it is human-readable text. JSON uses the key/value pair, and the key is enclosed in quotation marks followed by a colon and then the value like "id":"100". You use a comma (,) to separate multiple key/value pairs. Table 13.1 shows some examples.


TYPE

SAMPLE

Object

{

"id": "100",

"name": "Vacation"

}

Array with values only

["Family", "Friends", "Fun"]

Array with key/value

[

    {

       "id": "100",

       "name": "Vacation"

    },

    {

      "id": "102",

      "name": "Birthday"

    }

]

Object with array

{

   "id": "100",

   "name": "Vacation",

    "tags": ["Family", "Friends", "Fun"]

}

Multiple objects with arrays

{

   "journals":[

           {

              "id":"4710827",

              "mood":"Happy"

           }

 

,          {

              "id":"427836",

              "mood":"Surprised"

             },

      ],

  "tags":[

                 {

                     "id": "100",

                      "name": "Family"

                  },

               { "id": "102", "name": "Friends" }

 ]

 }

 

 

 

 

 


Introduction to JSON

What is JSON?


JSON stands for JavaScript Object Notation

JSON is a lightweight format for storing and transporting data

JSON is often used when data is sent from a server to a web page

JSON is "self-describing" and easy to understand


JSON Example

JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null.

The following example shows JSON data for a sample object that contains name-value pairs. The value for the name "firstName" is an array whose elements are two objects.


{
"employees":[
    {"firstName":"John""lastName":"Doe"},
    {"firstName":"Anna""lastName":"Smith"},
    {"firstName":"Peter""lastName":"Jones"}
]
}

JSON Syntax Rules

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON has the following syntax.

  • Objects are enclosed in braces ({}), their name-value pairs are separated by a comma (,), and the name and value in a pair are separated by a colon (:). Names in an object are strings, whereas values may be of any of the seven value types, including another object or an array.

  • Arrays are enclosed in brackets ([]), and their values are separated by a comma (,). Each value in an array may be of a different type, including another array or an object.

  • When objects and arrays contain other objects or arrays, the data has a tree-like structure.

JSON Object

A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma.

The { (curly brace) represents the JSON object.

{  

    "employee": {  

        "name":       "sonoo",   

        "salary":      56000,   

        "married":    true  

    }  




JSON Array

The [ (square bracket) represents the JSON array. A JSON array can have values and objects.

Let's see the example of a JSON array having values.


["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]


Let's see the example of a JSON array having objects.

[  

    {"name":"Ram", "email":"Ram@gmail.com"},  

    {"name":"Bob", "email":"bob32@gmail.com"}  

]


JSON Example

{"menu": {  

  "id": "file",  

  "value": "File",  

  "popup": {  

    "menuitem": [  

      {"value": "New", "onclick": "CreateDoc()"},  

      {"value": "Open", "onclick": "OpenDoc()"},  

      {"value": "Save", "onclick": "SaveDoc()"}  

    ]  

  }  

}}  


Comments

Popular posts from this blog

Unlocking the Power of OOP: A Beginner's Guide to Objects, Encapsulation, Inheritance, Abstraction, and Polymorphism

HTTP GET Response in Flutter

Building a Flutter Firebase Firestore CRUD App