DEV Community

Cover image for An Overview of JSON
Chidiadi Anyanwu
Chidiadi Anyanwu

Posted on • Originally published at thenetworkbits.substack.com

An Overview of JSON

Introduction

JSON is a popular file format used for data representation. It is easily readable by both humans and computers, and is commonly used with APIs and config files. It is also lightweight and integrates with most programming languages.

JSON means JavaScript Object Notation. This article talks about JSON data types and explores various ways it is used in development and deployment of apps and services.

History of JSON

JSON was first defined by Douglas Crockford in 2001 as a way to use a subset of JavaScript syntax to represent structured data.

"I discovered JSON. I do not claim to have invented it because it already existed in nature. ''
--Douglas Crockford

It picked up from there and became a popular.

Data types

JSON supports the following data types:

  • Strings
  • Numbers
  • Boolean
  • Null
  • Arrays
  • Objects

Strings: A string is a sequence of characters used to represent text. In JSON, strings can be used in key-value pairs or even in arrays, but must be enclosed in double quotes.

{
    "key":"Value  which is a string",
    "Cities":["Port Harcourt","Uyo","Lagos"]
}
Enter fullscreen mode Exit fullscreen mode

In the example above, Cities is a key also with an array of strings for its value.

Numbers: Numbers can be represented in JSON. Either integers or floating-point numbers. They can be negative, positive, zero or even exponential. In JSON, numbers are just written without enclosing them in anything.

{
        "integer": 23,
        "floating-point": 23.435,
        "exponential": 23e3,
        "negative": -45e-3
}
Enter fullscreen mode Exit fullscreen mode

Boolean: Boolean values are used to represent two states—true or false. Just like numbers, it is represented without enclosing in anything. The boolean values must be written in the lower case.

{
    "isSmart":true,
    "isComplete": false
}
Enter fullscreen mode Exit fullscreen mode

Null: JSON allows you to have an empty value where there is nothing for a particular key. It is represented as null. The null value is also not enclosed in any quotes, and is written in small letters.

{
    "name": null
}
Enter fullscreen mode Exit fullscreen mode

Arrays: Arrays are a way of representing an ordered list of values. The values can be strings, numbers, boolean or objects. In JSON, arrays are written as a comma-separated list in square brackets.

{
    //An array of user objects
    "users":[{"name":"John"},{"name":"Jane"},{"name":"James"}],

    //An array of numbers
    "list":[2,3,5,7,3,2,5],

    //An array of strings
    "countries":["Nigeria","Ghana","Kenya"],

    //An array of boolean values
    "answers":[true,false,false,true],

    //Arrays can also contain arrays in them
    "matrix":[
    [2,3],
    [3,4]
    ]
}
Enter fullscreen mode Exit fullscreen mode

Objects: Objects are used to represent structured data as collections of key-value pairs. Objects are enclosed in curly brackets {}. Each key-value pair must be separated by a comma, but the last pair does not need a comma after it. The keys must be strings while the values can take on any of the accepted data types mentioned above.

{
   //Create a JSON object for user
    "user":{
    "name": "Jagab",
    "age": 90,
    "id": 23746
    }
}
Enter fullscreen mode Exit fullscreen mode

Also, you would notice that in JSON, you must start with an object. So, in the example above, we have an object with one key-value pair which is user. That user is the key and the value is an object which is the user object with four key-value pairs in it.

Re-arranging it like this should make it clearer.

{
   //Create a JSON object for user
    "user":{ "name": "Jagab","age": 90,"id": 23746 }
}
Enter fullscreen mode Exit fullscreen mode

Uses of JSON

JSON is very popular, and is used in a variety of applications now. Some of them include:

  • Web APIs
  • Infrastructure as Code
  • Configuration files
  • Databases
  • Logging

Web APIs: RESTful APIs usually use JSON as a format to exchange data between client and server. For example, blog posts can be requested from a database, received as JSON and rendered on the website.

{
    "id":1,
    "title":"The Title",
    "author":{
        "id":12345,
        "name":"Chidi Ugwu",
        "email":"ugwuchid@example.com"
    },
    "content":"On a bright, sunny day..."
    "tags":["tag1","tag2"],
    "date_created":"2024-06-07"
}

Enter fullscreen mode Exit fullscreen mode

Infrastructure as Code: IaC tools usually use either JSON format or YAML to define or declare the infrastructure to deploy, and their properties. Here are some ways in which JSON is used:

  1. Azure Resource Manager (ARM): ARM Templates use JSON to define resources and infrastructure to be deployed on Azure.
{
    "$schema":"https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion":"1.0.0.0",
    "resources":[
        {
            "type":"Microsoft.Storage/storageAccounts",
            "apiVersion":"2019-06-01",
            "name":"mystorageaccount",
            "location":"eastus",
            "properties":{
                "accountType":"Standard_LRS"
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

In this example, a locally redundant Microsoft Storage Account named mystorageaccount is being provisioned in the East US region.

2.AWS CloudFormation: This is the AWS equivalent of the ARM Templates. AWS CloudFormation allows users to define infrastructure resources using JSON or YAML.

{
    "AWSTemplateFormatVersion":"2010-09-09",
    "Resources":{
        "EC2Instance":{
            "Type":"AWS::EC2::Instance",
            "Properties":{
                "ImadeId":"ami-abc123",
                "InstanceType":"t2.micro"    
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Configuration files: When building apps, you would need to define certain settings, options and parameters specific to the app. These are stored in configuration files. They could be in different formats: INI, JSON, YAML, XML or others.

JSON is a very popular choice of format for these types of files. An example of this is the storing of database connection settings in the config file.

{
    "appName":"Chidiadi's App",
    "version":"1.0",
    "database":{
        "host":"localhost",
        "port":"5432",
        "username":"root".
        "password":"password"
    }
}
Enter fullscreen mode Exit fullscreen mode

In the config files, there could also be a “scripts” section where CLI commands are automated. So, you have to use less commands to run more commands.

{
    "appName":"Chidiadi's App",
    "version":"1.0",
    "database":{
        "host":"localhost",
        "port":"5432",
        "username":"root".
        "password":"password"
    },
    "scripts":{
        "start":"node server.js",
        "install":"npm install"
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, typing start will run node server.js so you do not need to type out the whole thing.

Database: Because JSON data is easy to store, query and manipulate, it is also suitable for database applications where flexible schemas are required. Some database systems like couchDB, Amazon DocumentDB, Azure Cosmos DB, and Firebase Firestore use JSON files to store data.

Log Files: JSON is increasingly used for log files in applications due to its structured format, which is easily parsed and searchable. This makes it easy to ingest by log management tools.

If you enjoyed this article, please share it with others. Also subscribe for more.

Top comments (0)