6 min read
🔌beginner

Understanding JSON — The Language of Data Exchange

Learn what JSON is, how to read and write it, and why it is the most popular format for sending data between apps.

What is JSON?

JSON stands for JavaScript Object Notation. It is a simple text format for storing and exchanging data. Nearly every API in the world uses JSON to send data back and forth. JSON looks a lot like a JavaScript object (that is where the name comes from), but it is just text — any programming language can read it. It is human-readable too, which makes it easy to work with. Think of JSON as a universal language that all apps and services agree to speak when sharing data.

JSON Syntax

json
{
  "name": "Maya",
  "age": 12,
  "isStudent": true,
  "hobbies": ["coding", "drawing", "gaming"],
  "school": {
    "name": "Oakwood Middle",
    "grade": 7
  },
  "pet": null
}

JSON Rules and Data Types

JSON is strict about its format. Here are the rules: Data types you can use: - Strings: Must use double quotes ("hello", not 'hello') - Numbers: No quotes needed (42, 3.14) - Booleans: true or false (no quotes, lowercase) - Arrays: Lists in square brackets ([1, 2, 3]) - Objects: Key-value pairs in curly braces ({"key": "value"}) - Null: Represents 'nothing' (null) Important rules: - Keys MUST be strings in double quotes - No trailing commas allowed - No comments allowed (unlike JavaScript)

Working with JSON in JavaScript

javascript
// Convert a JavaScript object to a JSON string
let student = { name: 'Maya', age: 12, grade: 7 };
let jsonString = JSON.stringify(student);
console.log(jsonString);
// Output: {"name":"Maya","age":12,"grade":7}

// Convert a JSON string back to an object
let data = JSON.parse(jsonString);
console.log(data.name); // Maya
console.log(data.age);  // 12
Pro Tip

When debugging JSON, use a JSON formatter tool (search 'JSON formatter' online). These tools take messy, unformatted JSON and display it with nice indentation and color-coding. Most browsers also format JSON automatically if you visit a URL that returns JSON data.

Write Your Own JSON

Create a JSON file that describes your favorite video game. Include: the name (string), year released (number), your rating out of 10 (number), whether you have finished it (boolean), a list of genres (array of strings), and the developer as a nested object with name and country. Make sure your JSON is valid — try pasting it into jsonlint.com to check for errors!

Ready to build?

Put what you learned into practice — pick a project and start coding.

Start Building Free