JSON Introduction

What Is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based format for structured data. It uses key–value pairs and arrays that both humans and programs can read—the default format for most REST APIs.
JSON is text, not a live object:
Objects → { "key": "value" }
Arrays → [1, 2, 3]
Rules: double-quoted keys & strings · no comments · no trailing commas
JS: JSON.parse(text) ↔ JSON.stringify(obj) Despite the name, JSON is language-independent. Python, Java, PHP, Go, and many others all have parsers. It looks like a JavaScript object literal, but with stricter rules.
Why Use JSON?
Apps need a shared way to exchange structured data. JSON is compact, readable, and supported almost everywhere.
| Need | How JSON helps |
|---|---|
| API responses | Standard payload for REST and many GraphQL clients |
| Config files | Settings for apps, build tools, and CI pipelines |
| Easy for humans | Readable text you can inspect in DevTools |
| Easy for machines | Fast to parse in every major language |
How JSON Flows in a Web App
Typical path from server to your code:
| Step | What happens |
|---|---|
| 1. Send | Server returns a JSON text body (or you save a .json file) |
| 2. Receive | Client gets a string over HTTP |
| 3. Parse | JSON.parse() or response.json() builds objects in memory |
| 4. Use | Your code reads fields, updates the UI, or stringifies data to send back |
Your First JSON Object
A simple user record, then parse it in JavaScript:
{
"name": "John Doe",
"age": 30,
"city": "New York"
} const jsonString = '{"name":"John Doe","age":30,"city":"New York"}';
const data = JSON.parse(jsonString);
console.log(data.name); // John Doe
const text = JSON.stringify(data);
console.log(text); How It Works
1. Structure. Curly braces hold key–value pairs; keys and string values use double quotes.
2. Parse. JSON.parse turns the text into a live JavaScript object.
3. Stringify. JSON.stringify turns an object back into JSON text for storage or APIs.
Next, compare formats in JSON vs XML.
JSON vs XML
Both can carry structured data; JSON won most modern APIs:
| JSON | XML |
|---|---|
| Lightweight text | Heavier with opening/closing tags |
| Maps cleanly to objects/arrays | Document tree with attributes |
| No comments in standard JSON | Supports comments |
| Default for most REST APIs | Still used in SOAP, RSS, and legacy systems |
When to choose which
| Prefer JSON when… | Prefer XML when… |
|---|---|
| You build web or mobile APIs | You integrate with SOAP or older enterprise systems |
| You want less verbose payloads | You need schemas and namespaces already in XML |
| Your client is JavaScript-heavy | Your industry standard still requires XML |
JSON Data Types
Only these value types are valid in JSON:
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Must use double quotes |
| Number | 42, 3.14 | Integer or decimal |
| Boolean | true, false | Lowercase only |
| null | null | Empty or missing value |
| Array | [1, 2, 3] | Ordered list of values |
| Object | {"key": "value"} | Collection of key–value pairs |
Where Is JSON Used?
JSON shows up across modern software:
| Use case | Why JSON fits |
|---|---|
| Web & mobile APIs | Compact responses browsers and apps already understand |
| Configuration | package.json, app settings, CI configs |
| Document databases | MongoDB and similar stores JSON-like documents |
| Logging & analytics | Structured events that tools can index |
| Microservices | Shared message format between services |
| Data export | Easy interchange between apps and languages |
Key Takeaways
- JSON is a text data format—not executable JavaScript.
- Rules: double quotes, no comments, no trailing commas.
- JS helpers:
parsereads JSON;stringifywrites it. - Everywhere: APIs, configs, and document stores rely on it.
One line: JSON is the web’s shared data text—next, compare JSON vs XML.
Frequently Asked Questions
Did you know?
JSON uses double-quoted keys and strings, objects in { }, and arrays in [ ]. In JavaScript, convert text to data with JSON.parse() and data to text with JSON.stringify().
Next: JSON vs XML
See how JSON compares to XML for APIs and documents.
10 people found this page helpful
