DEV Community

Cover image for What is JSON?
Bhartee Rameshwar Sahare
Bhartee Rameshwar Sahare

Posted on

What is JSON?

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:

  1. It is supported by all programming languages.
  2. It can be used on all platforms.
  3. It is a lightweight database standard, so data transmission is fast.

Where JSON Is Used:

  1. Web Application
  2. API's
  3. Mobile application
  4. Gaming Api's
  5. IOT

Data Type Supported:

  1. String
  2. Number
  3. Array
  4. Null
  5. Boolean
  6. Object

JSON Syntax and Datatype:

  1. Every piece of JSON data is enclosed in curly brackets.
  2. Key-value pairs are used to describe JSON data.
  3. Always surround the word "key" in double quotes.
  4. Keys and values must always be separated by a comma (:).
  5. Values should be expressed correctly; double quotes for Strings are
  6. preferred over Boolean quotes for Numbers.
  7. Commas (,) should be used to divide text.
  8. 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"
          }
      }
  }
]
Enter fullscreen mode Exit fullscreen mode

JSON Array Declaration:

[
  "computer_science", "mechanical_engineer", "electrical_engineer"
]
Enter fullscreen mode Exit fullscreen mode

JSON Object Declaration:

{
  "id": 1,
  "name": "test",
  "class": "1st",
  "dob": "1970-02-15"
  "certicate_achieve": ["rest_api", "json", "ruby"]
}
Enter fullscreen mode Exit fullscreen mode

Converting JSON Text to a JavaScript Object:

  1. JSON.parse(): Take the JSON string and convert it into a javascript object.
  2. 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'}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)