What is JSON Used for


We have learned about JSON earlier but for a brief intro, JSON is a lightweight data-interchange format that has become widely used in modern web development.

In this section, we will take a closer look at the use of JSON in different fields of technology.

    Table of Contents

  1. Use of JSON
    1. JSON in Web Development
    2. JSON in Mobile Development
    3. JSON in Database
    4. JSON in IoT
    5. JSON in Configuration
    6. JSON in Logging
  2. Conclusion

Use of JSON

Now a days, JSON is used in almost every field of technology. There is no doubt that JSON is the most popular data-interchange format in the world.

It's use can't be summarized in a single article, we will try to cover most of the use cases of JSON in this section.


1. JSON in Web Development

When it comes to web development, JSON is used in almost every field of web development. From client-side data storage in local storage to server-side data storage in database combined with data interchange between client and server, JSON is used everywhere.

Following is an example of using JSON to send data from a server-side script (in this case, a PHP script) to a client-side JavaScript script:

Server-side script (PHP):

<?php
    $data = array("name" => "Herry", "age" => 20);
    echo json_encode($data);
?>

Client-side script (JavaScript):

var xhr = new XMLHttpRequest();
xhr.open('GET', 'data.php', true);
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        console.log(data.name + " is " + data.age + " years old.");
    }
};
xhr.send();

Here, we are using PHP to send data from server to client in JSON format and then using JavaScript to parse the JSON data and display it on the console.


2. JSON in Mobile Development

In mobile development JSON is used in mobile apps to store data locally in the device and also to send data from server to client.

Other than that, JSON is also used in mobile apps for:

Use Example
API Communication Mobile app may use a JSON-formatted payload to send a POST request to a server to create a new user account and receive a JSON-formatted response indicating the success or failure of the request.
Data Persistence Mobile apps may use JSON to store user preferences or app settings on the device.
Push Notifications A server may use JSON to send a message to a mobile app indicating that a new message has been received
Offline support Apps may use JSON to store data locally on the device when an internet connection is not available, and then sync this data with the server when a connection is restored.

3. JSON in Database

JSON is also used to store and retrieve data from the database. Many modern databases, such as MongoDB and Couchbase store data as JSON.

In these databases, data is stored in documents, which are JSON objects. These documents are stored in collections, which are similar to tables in relational databases.

Following is an example of using JSON to store data in the MongoDB database:

db.collection.insertOne(
    {
        "name": "Herry",
        "age": 20
    }
);

Here, we are using MongoDB to store data in JSON format.


4. JSON in IoT

JSON is widely used in the Internet of Things (IoT) for data exchange between IoT devices and servers. IoT devices often have limited resources, such as memory and processing power, so JSON is used as a lightweight and efficient format for data transfer.

Here is an example of using JSON to send sensor data from an IoT device (in this case, an ESP8266 microcontroller) to a server:

IoT Device (C++):

#include 

void sendSensorData() {
    StaticJsonBuffer<200> jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    root["sensor_id"] = 1;
    root["temperature"] = 25.6;
    root["humidity"] = 40.2;

    String jsonString;
    root.printTo(jsonString);

    WiFiClient client;
    if (client.connect("example.com", 80)) {
        client.print("POST /sensors HTTP/1.1\r\n");
        client.print("Content-Type: application/json\r\n");
        client.print("Content-Length: ");
        client.print(jsonString.length());
        client.print("\r\n\r\n");
        client.print(jsonString);
    }
    client.stop();
}

Server (PHP):

<?php
    $data = json_decode(file_get_contents("php://input"), true);
    $sensor_id = $data["sensor_id"];
    $temperature = $data["temperature"];
    $humidity = $data["humidity"];

    //... save data to database
?>

Here, we are using JSON to send sensor data from an IoT device to a server.


5. JSON in Configuration

JSON is often used as a format for storing configuration settings in applications. Its hierarchical structure makes it well suited for storing nested key-value pairs, making it easy to organize and access configuration settings.

Here is an example of using JSON to store configuration settings for a Node.js application:

config.json:

{
    "server": {
        "port": 8080,
        "host": "localhost"
    },
    "database": {
        "username": "admin",
        "password": "password",
        "host": "localhost",
        "port": 27017,
        "name": "mydb"
    }
}

app.js:

var config = require("./config.json");
var server = require("http").createServer();
server.listen(config.server.port, config.server.host);

In the above example, a JSON file called "config.json" is used to store the configuration settings for a Node.js application. The file contains nested key-value pairs for the server and database configuration settings. In the app.js file, the config.json file is imported using the require() function and the config object is used to set the server's port and host.


6. JSON in Logging

Logging is the process in which an application records the events that occur to refer later. JSON is suitable for storing log data because it is lightweight and easy to parse.

Most of the application use JSON to store log data because both lightweight and easy to parse.


Conclusion

In conclusion, JSON is a flexible and popular data format that is essential to many different technological fields. It is used to store, transfer, and visualize data across a variety of industries, including databases and the Internet of Things.

It is a well-liked option for data interchange in communication protocols, APIs, and logging due to its lightweight, simple-to-parse nature.

Overall, it is a crucial tool in the technological landscape of the present day, facilitating data work and system connections for developers.