0
0
Rest APIprogramming~5 mins

JSON as standard format in Rest API

Choose your learning style9 modes available
Introduction

JSON is a simple way to send data between computers. It is easy to read and write for both people and machines.

When sending data from a web server to a web browser.
When two different programs need to share information.
When storing data in a file that can be used later.
When building APIs that allow apps to talk to each other.
When you want a format that works on many devices and languages.
Syntax
Rest API
{
  "key": "value",
  "number": 123,
  "list": [1, 2, 3],
  "object": {"innerKey": "innerValue"}
}

JSON uses curly braces {} for objects and square brackets [] for lists.

Keys and string values must be in double quotes.

Examples
A simple JSON object with two keys: name and age.
Rest API
{ "name": "Alice", "age": 30 }
A JSON array (list) of strings.
Rest API
["apple", "banana", "cherry"]
A JSON object containing another object inside it.
Rest API
{ "user": { "id": 1, "active": true } }
Sample Program

This program creates a Python dictionary and converts it to a JSON string using the json.dumps() function. Then it prints the JSON string.

Rest API
import json

# Create a Python dictionary
person = {
    "name": "Bob",
    "age": 25,
    "hobbies": ["reading", "gaming", "hiking"]
}

# Convert dictionary to JSON string
json_data = json.dumps(person)

# Print JSON string
print(json_data)
OutputSuccess
Important Notes

JSON is text only, so it can be sent easily over the internet.

Always use double quotes for keys and string values in JSON.

JSON does not support comments inside the data.

Summary

JSON is a simple, text-based format to share data.

It uses objects (curly braces) and arrays (square brackets).

It is the standard format for REST APIs to exchange information.