JSON

background image

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript programming language, and it is used to transmit data between a server and a web application or between two systems.

A JSON object is a collection of key-value pairs, where the keys are strings and the values can be strings, numbers, booleans, arrays, or other objects. JSON objects are surrounded by curly braces {} and are separated by commas.

Here is an example of a JSON object:

{
  "name": "John",
  "age": 30,
  "city": "New York"
}

A JSON array is an ordered collection of values. JSON arrays are surrounded by square brackets [] and are separated by commas.

Here is an example of a JSON array:

["apple", "banana", "orange"]

JSON is often used to transmit data between a server and a web application, as it is lightweight and easy to parse. It is also a common format for storing structured data in databases and for configuration files.

You can use the JSON.parse() function in JavaScript to parse a JSON string and convert it into a JavaScript object, and you can use the JSON.stringify() function to convert a JavaScript object into a JSON string.

For example:

// Convert a JSON string into a JavaScript object
const object = JSON.parse('{"name": "John", "age": 30, "city": "New York"}');

// Convert a JavaScript object into a JSON string
const json = JSON.stringify({ name: "John", age: 30, city: "New York" });