How to Use JSON: A Beginner's Guide with Examples
You use JSON to organise structured data and exchange it between applications. This example contains three pieces of information about Rahul: name, city, and age.
By Archana Bhandari ·
A first JSON example
JSON is plain text with a few simple rules. Software can read the labels and values, then use them where needed.
{
"name": "Rahul",
"city": "Dehradun",
"age": 28
}Before you start: what is JSON?
JSON stands for JavaScript Object Notation. It is a data format, not a programming language. If you are completely new to it, read the short JSON basics guide first.
How to write JSON
Start with one object. Curly brackets { } create the object, "name" is the key, and "Rahul" is the value. JSON strings always use double quotes.
{
"name": "Rahul"
}- Add another property with a comma after the previous value.
- Do not add a comma after the final property.
- Keep keys and text values in double quotes.
Adding more information
This object has three properties. Commas are required because they separate one key-value pair from the next.
{
"name": "Rahul",
"age": 28,
"city": "Dehradun"
}Using different types of data in JSON
A string is text, a number is a numeric value, a boolean is true or false, and null means a value is not available. Write true, false, and null without quotes when you mean those JSON values.
{
"name": "Rahul",
"age": 28,
"isDeveloper": true,
"company": null
}How to use JSON objects
Use a nested object when one item has its own labelled details. You can think of user as one folder, with name and city inside it. This keeps related information together.
{
"user": {
"name": "Rahul",
"city": "Dehradun"
}
}How to use JSON arrays
An array is useful when you have a list of values. Square brackets [ ] hold the list.
{
"cities": [
"Delhi",
"Dehradun",
"Mumbai"
]
}- An array can also contain objects. That is useful for a list of similar records, such as users.
An array of JSON objects
This structure lets an application keep many users in one list, while each user still has labelled details.
{
"users": [
{
"name": "Rahul",
"city": "Dehradun"
},
{
"name": "Amit",
"city": "Delhi"
}
]
}A real-world JSON example
An online shop could use this JSON to describe a product. id identifies it, price is its cost, inStock tells the site whether it can be ordered, and colors is a list of choices. An e-commerce website or its API can use these values to build a product page.
{
"id": 101,
"name": "Wireless Mouse",
"price": 799,
"inStock": true,
"colors": [
"Black",
"White"
]
}How JSON is used with APIs
An API is a way for one application to ask another application for information. For example, a weather app asks a weather service for the current temperature. The flow is: App → request → API → JSON response → app displays information. You do not need to know HTTP details to understand the JSON response.
{
"city": "Dehradun",
"temperature": 27,
"condition": "Cloudy"
}How to create a JSON file
Create a normal text file, add valid JSON, and save it with a .json extension, such as users.json. JSON files are plain text, so common text and code editors can open them.
{
"users": [
{ "name": "Rahul" }
]
}How to read JSON
Read the key, then its value. Here, name means Sonia, and skills is a list containing PHP, JavaScript, and WordPress. Indentation makes JSON easier for people to read but generally does not change the underlying data.
{
"name": "Sonia",
"skills": [
"PHP",
"JavaScript",
"WordPress"
]
}Format one-line JSON
JSON can be valid even when it arrives on one long line: {"name":"Sonia","skills":["PHP","JavaScript","WordPress"]}. If your JSON arrives like this, paste it into JSONPlease JSON Formatter to make it easier to read. Formatting changes the layout, not the data.
How to check if your JSON is valid
Valid JSON follows the quote, comma, and bracket rules. This example is invalid because standard JSON does not allow a trailing comma after the final property. Paste JSON into the JSONPlease JSON Validator to locate syntax problems. If the data has common formatting mistakes, JSON Repair can help correct them; both tools process it locally in your browser.
{
"name": "Rahul",
"city": "Dehradun",
}Common rules to remember when writing JSON
Most problems disappear when you check these rules before saving a JSON file.
- Use double quotes for keys and strings.
- Separate properties with commas, but not after the last property.
- Use { } for objects and [ ] for arrays.
- Use true, false, and null without quotes when they represent JSON values.
- Close every opening brace and bracket.
Common JSON mistakes
The usual mistakes are single quotes, a missing or extra comma, a missing quote, incorrect brackets, and an unescaped double quote inside text. Another common error is pasting JavaScript object syntax into a JSON file: JavaScript can allow unquoted keys or trailing commas, but standard JSON cannot.
{
"message": "She said \"Hello\""
}Where will you use JSON?
You will see JSON in APIs that return data to websites and mobile apps, configuration files, browser storage, data import and export features, and API testing tools. In each case, it provides a predictable shape for data to move from one place to another.
- Web and mobile applications use JSON to load information.
- Configuration files store settings in a readable form.
- Import and export features use it to exchange structured records.
- Testers inspect API JSON responses to check whether an application returned the expected data.
Is JSON difficult to learn?
Basic JSON is relatively straightforward because there are only a few structural rules. Start with a small object, then practise objects, arrays, quotes, commas, and brackets. You do not need to memorise everything at once.
Practice example
Try creating JSON for a person with name, age, city, skills, and isEmployed. Then compare your work with this valid example.
{
"name": "Sonia",
"age": 29,
"city": "Jaipur",
"skills": ["PHP", "WordPress"],
"isEmployed": true
}Frequently asked questions
How do I start using JSON? Begin with a small object, then add labelled values one at a time.
How do I write JSON correctly? Use double quotes, correct commas, and matching brackets.
Can I create JSON in Notepad or a text editor? Yes. Save the plain-text file with a .json extension.
Does JSON require double quotes? Yes, for keys and text strings.
Can JSON contain arrays? Yes. Arrays are lists inside square brackets.
Can JSON contain objects inside objects? Yes. These are nested objects.
Can I use comments in JSON? No. Standard JSON does not allow comments.
How do I check if JSON is valid? Use a JSON validator.
What program opens a JSON file? Any normal text or code editor can open it.