JSON Tutorial

Last Updated : 22 Oct, 2026

JSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data.

  • Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.
  • Text-based: JSON is a simple text format, making it lightweight and easy to transmit.
  • Human-readable: It uses key-value pairs, making the structure easy to understand.
  • Language-independent: While it is derived from JavaScript, JSON is supported by many programming languages including Python, Java, PHP, and more.
  • Supported Data structures: Represents data as objects, arrays, strings, numbers, booleans, and null.

JSON Data Flow: From Server to Client

web_team

JSON vs XML

When it comes to data formats, JSON and XML are the two most common choices. JSON is generally preferred for web applications due to its smaller size, ease of use, and better performance. Here's a quick comparison:

Working of JSON Data Flow in Web Applications

JSON enables data exchange between the client (frontend) and server in a web application. It allows different technologies to communicate using a common data format.

  • Server Side: Data is converted into a JSON string and sent to the client through an API response.
  • Client Side: The received JSON data is parsed into a native object, allowing the application to access and use individual values.

Example JSON String Received from Server

{"name":"Alen", "age":30}

This JSON data contains:

  • name: "Alen"
  • age: 30

JSON Structure

The basic structure of JSON consists of two primary components:

  • Objects: These are enclosed in curly braces {} and contain key-value pairs.
  • Arrays: Arrays are ordered lists enclosed in square brackets [].

Parsing JSON in Different Programming Languages

Although the JSON data is the same, each language uses its own method or library to parse it.

C++
#include <iostream>
#include <string>
#include <nlohmann/json.hpp>
using namespace std;

using json = nlohmann::json;

int main() {
    string json_s = R"({"name": "Alen", "age": 30})";
    json obj = json::parse(json_s);
    string name = obj["name"];
    int age = obj["age"];
    cout << "Name: " << name << ", Age: " << age << endl;
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <jansson.h>

int main() {
    const char *json_s = "{\"name\": \"Alen\", \"age\": 30}";
    json_error_t error;
    json_t *root = json_loads(json_s, 0, &error);
    if (!root) {
        fprintf(stderr, "Error: on line %d: %s\n", error.line, error.text);
        return 1;
    }
    const char *name = json_string(json_object_get(root, "name"));
    int age = json_integer_value(json_object_get(root, "age"));
    printf("Name: %s, Age: %d\n", name, age);
    json_decref(root);
    return 0;
}
Java
import org.json.JSONObject;

public class Main {
    public static void main(String[] args) {
        String json_s = "{\"name\": \"Alen\", \"age\": 30}";
        JSONObject obj = new JSONObject(json_s);
        String name = obj.getString("name");
        int age = obj.getInt("age");
        System.out.println("Name: " + name + ", Age: " + age);
    }
}
Python
import json

json_s = '{"name": "Alen", "age": 30}'

obj = json.loads(json_s)
name = obj["name"]
age = obj["age"]

print(f"Name: {name}, Age: {age}")
C#
using System;
using Newtonsoft.Json;

public class Program {
    public static void Main() {
        string json_s = "{\"name\": \"Alen\", \"age\": 30}";
        dynamic obj = JsonConvert.DeserializeObject(json_s);
        string name = obj.name;
        int age = obj.age;
        Console.WriteLine("Name: " + name + ", Age: " + age);
    }
}
JavaScript
const json_s = '{"name": "Alen", "age": 30}';

const obj = JSON.parse(json_s);
const name = obj.name;
const age = obj.age;

console.log(`Name: ${name}, Age: ${age}`);

Fundamentals

Methods and Operations

JSON API Integration

Tools and Setup

JSON in Programming Languages

JSON Formats and Comparisons

Advanced Concept

Reference and Interview Preparation

Applications of JSON

  • APIs: JSON is the most commonly used format for API responses due to its lightweight nature.
  • Configuration Files: Many software systems use JSON files for storing configuration data.
  • Databases: Some NoSQL databases, like MongoDB, store data in JSON-like formats.
  • Data Transfer: JSON is widely used for transferring data between servers and clients, especially in web development.
Comment