What is JSON?
JSON (JavaScript Object Notation) is a lightweight, human-readable data format for storing and exchanging data. It’s widely used in APIs, configurations, and data storage. The file extension for JSON is .json, and the MIME type is application/json.
The following is an example of JSON data representing the information of the JavaScript programming language.
{
"name": "JavaScript",
"creator": "Brendan Eich",
"year": 1995,
"extensions": [".js", ".mjs"]
}JSON Syntax
- Data is structured as
"key": "value" - Comma separates items
, - Objects use curly braces
{} - Arrays use square brackets
[]
Supported Data Types
- String: Text in double quotes
"key": "value" - Number: Includes integers and floats
1 2 3 4.5 - Boolean: true or false
"true": "false" - Array: A list of values
[] - Object: Key-value pairs
{} - null: Represents no value
null
Common Use Cases
JSON is popular for API responses and configuration files:
{
"host": "example.com",
"port": 8080,
"secure": true
}How to Parse JSON in JavaScript
Use JSON.parse() to convert a JSON string into an object:
const jsonString = '{"name": "Alice", "age": 30}';
const user = JSON.parse(jsonString);
console.log(user.name); // AliceHow to Convert JavaScript to JSON
Use JSON.stringify() to turn a JS object into a JSON string:
const user = { name: "Alice", age: 30 };
const json = JSON.stringify(user);
console.log(json);Why Format JSON?
Minified JSON is hard to read. JSON formatting tools help you beautify the content so it's readable and maintainable.
Why Minify JSON?
Minifying JSON removes whitespace and line breaks, making it smaller for faster transfers and lower storage usage.
{"name":"Alice","age":30}