Let's Learn JSON Together

Let's Learn JSON Together

JSON stands for JavaScript Object Notation

JSON is an open standard lightweight data-interchange format that uses human-readable text to store and transmit data. It is commonly used for transmitting data in web applications.

Why the name “JavaScript Object Notation”?

→ JSON uses the basic idea of javascript data structure. Its origin is based on how JavaScript objects work, so in that sense, it is related. Regardless of the fact that it has its origin from JavaScript, it is widely used across many Languages.

Structure of JSON

Write given data in JSON format

  1. John, Williams, 30
  2. Glory, Williams, 35
{

  "Students": [

    {

      "firstName": "John",

      "lastName": "Williams",

      "age": 30

    },

    {

      "firstName": "Glory",

      "lastName": "Williams",

      "age": 35

    }

  ]

}

It is easier to manage, update, and also to search anything both by humans and programming languages.

JSON Structures

  1. A collection of key-value pair, i.e. Object
  2. An ordered list of values, i.e. Array

Rules of JSON

  • Data is in key-value pairs
  • Data is separated by commas (,)
  • Curly braces {} hold objects
  • Square brackets [] hold arrays

---------------------------------------------------------------------------------------------------------------

A key/value pair consists of a field name or key (in double quotes), followed by a colon, followed by a value

Example →

"firstName": "John"
  1. In JSON, keys must be strings, written with double quotes.
  2. In JSON, values can be of type
  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • Null

 --------------------------------------------------------------------------------------------------------------

All data is separated by a comma(,)

Example →

      "firstName": "John",

      "lastName": "Williams",

      "age": 30

---------------------------------------------------------------------------------------------------------------

Curly braces {} hold objects or we can say JSON objects are surrounded by curly braces {}

Example → This is one object.

{

      "firstName": "John",

      "lastName": "Williams",

      "age": 30

}

There can be nested objects i.e. objects within the object and inside these objects JSON rules remains the same.

  • Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
  • Keys and values are separated by a colon.
  • Each key/value pair is separated by a comma.

 Access an Object

To access the object values use dot(.) or square brackets [] in programming.

Example → 

studentObject = { "firstName": "John", "lastName": "Williams", "age": 30 };
Using dot(.)
x = studentObject.firstName;


Using square brackets []
x = studentObject["firstName"];

---------------------------------------------------------------------------------------------------------------

Square brackets [] hold arrays OR we can say Arrays are surrounded by Square Brackets []

Example →

{

  "Students": [

    {

      "firstName": "John",

      "lastName": "Williams",

      "age": 30

    }

  ]

}

Access an Object from Array

To access the Object from Array, store the complete Students array in a variable say myStudents

student = myStudents.Students[0]

JSON Validator

Once JSON is created, you can beautify and validate it using different online tools like https://jsonlint.com/. In case of invalid JSON, validator will give you error.

JSON Validator

What is JSON Path?

It is the location or address or a specific value or data in a JSON. JSONPath is a query language for JSON, similar to XPath for XML. JSONPath helps to parse JSON data. You can create a JSON Path manually or use tools like JSON Path Finder

JSON Path Example:

x.Students[0].firstName

What is Media type?

Media type or MIME-type is a label used to identify a type of data. It is used so the software can know how to handle the data. So when you send data, you also send its media type so that the server will know what type of data you are sending and can parse it accordingly.

JSON Schema

JSON Schema is a JSON Document that describes other JSON Documents. It defines the structure of a JSON message. It can be used to validate a JSON message or API request and response in JSON format. You can create JSON Schema manually or use tools like JSON to JSON Schema Converter

Sample JSON

{

  "users": [

    {

      "name": "John",

      "age": 25

    }

  ]

}



JSON Schema for above Sample JSON

{

  "$schema": "http://json-schema.org/draft-04/schema#",

  "type": "object",

  "properties": {

    "users": {

      "type": "array",

      "items": [

        {

          "type": "object",

          "properties": {

            "name": {

              "type": "string"

            },

            "age": {

              "type": "integer"

            },

          "required": [

            "name",

            "age"

          ]

        }

      ]

    }

  },

  "required": [

    "users"

  ]

}

You can use this tool to validate your JSON against schema: https://www.jsonschemavalidator.net/

Key Points To Remember

  1. JSON stands for JavaScript Object Notation.
  2. JSON is a syntax or format for storing and exchanging data.
  3. JSON is language independent.
  4. JSON is like One universal language for communication between programming languages and applications
  5. Platforms or applications built over different languages can exchange data or information using JSON.
  6. The file type or file extension for JSON files is “.json”
  7. The Media type for JSON text is “application/json”
  8. You will find a function JSON.parse() in all languages that can communicate with JSON format

To view or add a comment, sign in

More articles by Jyoti Jindal

  • Time Complexity

    If you ever appeared in a Tech Interview, your interviewer must have asked you to identify the running time complexity…

  • Implementation of Java substring() in Java 6, and why it was changed in Java 7?

    String is a special class in Java and substring() method of String class is one of the widely used methods of String…

  • HashMap

    HashMap is the ultimate choice for our application if it demands faster insertion and retrieval. While selecting the…

    11 Comments
  • Searching Algorithms

    Searching Algorithms are designed to find an element or retrieve an element from any data structure where it is stored.…

    4 Comments
  • Flowchart - A Diagrammatic Representation of an Algorithm

    Introduction Here are the steps to be followed to solve an algorithmic problem: Analysing the problem statement and…

  • Redis - An in-memory database

    What is Redis? Redis (Remote Dictionary Server) is an open source, in-memory data structure store, used as a database…

Others also viewed

Explore content categories