JSON (JavaScript Object Notation) is a lightweight data-interchange format that is commonly used in RESTful web services. JSON is easy to read and write, and is supported by most modern programming languages, including Java.
JSON is based on a key-value pair format and can represent simple or complex data structures. JSON data is often sent between clients and servers in HTTP requests and responses. In RESTful web services, JSON is commonly used as the data format for request and response payloads.
For example, let’s say we have a RESTful web service that manages orders for a coffee shop. When a client creates a new order, it might send a JSON payload like this:
POST /orders HTTP/1.1
Host: example.com
Content-Type: application/json
{
"customer\_id": 123,
"items": [
{
"name": "Cappuccino",
"quantity": 1
},
{
"name": "Croissant",
"quantity": 2
}
]
}
In this example, the client sends a JSON payload that includes the customer ID and a list of items in the order. The server can then parse the JSON payload and use the data to create a new order in the system.
When the server responds to the client, it might send a JSON payload that includes the details of the new order:
HTTP/1.1 201 Created Content-Type: application/json
"id": 456, "customer_id": 123, "items": [ "name": "Cappuccino", "quantity": 1, "price": 2.99 , "name": "Croissant", "quantity": 2, "price": 2.00 ], "total": 6.99
In this example, the server sends a JSON payload that includes the details of the new order, including the order ID, customer ID, list of items, and total price.
In summary, JSON is a lightweight data-interchange format that is commonly used in RESTful web services to represent request and response payloads. JSON data is easy to read and write, and is supported by most modern programming languages, including Java.