JSON Format Tutorial
Introduction
What is JSON?
JSON, or 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. JSON is a text format that is completely language-independent but uses conventions familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
Example
In this JSON example:
The person's name, email, and address fields are strings.
The age is a number.
The isStudent field is a boolean.
The courses field is an array of strings.
The address field is an object containing the street, city, state, and zipCode fields
Why JSON?
JSON is often used to transmit data between a server and a web application, serving as an alternative to XML. JSON is favored because it is:
Easy to read and write for humans
Easy to parse and generate for machines
Less verbose than XML
Natively supported in JavaScript
Widely supported in many programming languages
JSON Syntax
JSON syntax is a set of rules for writing JSON data. JSON data is represented using two structures:
A collection of key-value pairs (also known as an object)
An ordered list of values (also known as an array)
The key features of JSON syntax are:
Data is stored in key-value pairs, separated by colons (
:
)Curly braces (
{}
) hold objectsSquare brackets (
[]
) hold arraysKeys must be strings wrapped in double quotes (
"
)Values can be strings, numbers, booleans, objects, or arrays
Multiple key-value pairs in an object, as well as multiple values in an array, are separated by commas (
,
)
JSON Data Types
JSON supports the following data types:
String: A sequence of characters enclosed in double quotes, e.g., "hello"
Number: A number without quotes, e.g., 42
or 3.14
. JSON numbers can be integers or floating-point values.
Boolean: Represents either true
or false
(without quotes)
Array: An ordered collection of values enclosed in square brackets, e.g., ["apple", "banana", "cherry"]
Object: A collection of key-value pairs enclosed in curly braces, e.g., {"name": "John", "age": 30}
null: Represents an empty value or non-existence (without quotes)
JSON Objects and Arrays
JSON Objects
A JSON object is a collection of key-value pairs, where each key is a string, and the value can be any JSON data type. JSON objects are wrapped in curly braces ({}
). For example:
JSON Arrays
A JSON array is an ordered collection of values, where each value can be any JSON data type. JSON arrays are wrapped in square brackets ([]
). For example:
Last updated