[Avg. reading time: 8 minutes]

JSON

Java Script Object Notation.

  • This is neither a row-based nor Columnar Format.
  • The flexible way to store & share data across systems.
  • It's a text file with curly braces & key-value pairs { }

Simplest JSON format

{"id": "1","name":"Rachel"}

Properties

  • Language Independent.
  • Self-describing and easy to understand.

Basic Rules

  • Curly braces to hold the objects.
  • Data is represented in Key Value or Name Value pairs.
  • Data is separated by a comma.
  • The use of double quotes is necessary.
  • Square brackets [ ] hold an array of data.

JSON Values

String  {"name":"Rachel"}

Number  {"id":101}

Boolean {"result":true, "status":false}  (lowercase)

Object  {
            "character":{"fname":"Rachel","lname":"Green"}
        }

Array   {
            "characters":["Rachel","Ross","Joey","Chanlder"]
        }

NULL    {"id":null}

Sample JSON Document

{
    "characters": [
        {
            "id" : 1,
            "fName":"Rachel",
            "lName":"Green",
            "status":true
        },
        {
            "id" : 2,
            "fName":"Ross",
            "lName":"Geller",
            "status":true
        },
        {
            "id" : 3,
            "fName":"Chandler",
            "lName":"Bing",
            "status":true
        },
        {
            "id" : 4,
            "fName":"Phebe",
            "lName":"Buffay",
            "status":false
        }
    ]
}

JSON Best Practices

No Hyphen in your Keys.

{"first-name":"Rachel","last-name":"Green"}  is not right. ✘

Under Scores Okay

{"first_name":"Rachel","last_name":"Green"} is okay ✓

Lowercase Okay

{"firstname":"Rachel","lastname":"Green"} is okay ✓

Camelcase best

{"firstName":"Rachel","lastName":"Green"} is the best. ✓

Use Cases

  • APIs and Web Services: JSON is widely used in RESTful APIs for sending and receiving data.

  • Configuration Files: Many modern applications and development tools use JSON for configuration.

  • Data Storage: Some NoSQL databases like MongoDB use JSON or BSON (binary JSON) for storing data.

  • Serialization and Deserialization: Converting data to/from a format that can be stored or transmitted.

Python Example

Serialize : Convert Python Object to JSON (Shareable) Format.

DeSerialize : Convert JSON (Shareable) String to Python Object.


import json

def json_serialize(file_name):
    # Python dictionary with Friend's characters
    friends_characters = {
        "characters": [{
            "name": "Rachel Green",
            "job": "Fashion Executive"
        }, {
            "name": "Ross Geller",
            "job": "Paleontologist"
        }, {
            "name": "Monica Geller",
            "job": "Chef"
        }, {
            "name": "Chandler Bing",
            "job": "Statistical Analysis and Data Reconfiguration"
        }, {
            "name": "Joey Tribbiani",
            "job": "Actor"
        }, {
            "name": "Phoebe Buffay",
            "job": "Massage Therapist"
        }]
    }

    print(type(friends_characters), friends_characters)

    print("-" * 200)

    # Serializing json
    json_data = json.dumps(friends_characters, indent=4)
    print(type(json_data), json_data)

    # Saving to a file
    with open(file_name, 'w') as file:
        json.dump(friends_characters, file, indent=4)


def json_deserialize(file_name):
    #file_path = 'friends_characters.json'

    # Open the file and read the JSON content
    with open(file_name, 'r') as file:
        data = json.load(file)
    print(data, type(data))


def main():
    file_name = 'friends_characters.json'
    json_serialize(file_name)
    json_deserialize(file_name)


if __name__ == "__main__":
    print("Starting JSON Serialization...")
    main()
    print("Done!")

#bigdata #dataformat #json #hierarchicalVer 5.5.3

Last change: 2025-10-15