In this article · 7 sections
When your website fetches fares, a payment gateway confirms a payment, or Google reads your structured data, the data is usually in JSON (JavaScript Object Notation). It's a lightweight text format that's easy for humans to read and for programs to parse. Here's JSON explained from scratch.
What JSON looks like
{
"business": "Joboo Taxi",
"city": "Ludhiana",
"rating": 4.8,
"ac": true,
"cars": ["Dzire", "Ertiga", "Innova Crysta"],
"owner": { "name": "Aman", "phone": "9876543210" },
"gst": null
}
Syntax rules
- Data is in key/value pairs:
"key": value - Keys must use double quotes
- Values can be: string, number, boolean (
true/false),null, object{}, array[] - No trailing commas and no comments
- Strings use double quotes, not single quotes
Common errors
| Mistake | Wrong | Right |
|---|---|---|
| Single quotes | {'city': 'Delhi'} |
{"city": "Delhi"} |
| Trailing comma | ["a","b",] |
["a","b"] |
| Unquoted key | {city: "Delhi"} |
{"city": "Delhi"} |
Use an online JSON validator or your editor's formatter to find errors quickly.
JSON in PHP
<?php
$route = ['from' => 'Ludhiana', 'to' => 'Delhi', 'fare' => 3700];
header('Content-Type: application/json');
echo json_encode($route, JSON_UNESCAPED_UNICODE);
$data = json_decode('{"from":"Ludhiana","fare":3700}', true); // true = associative array
echo $data['fare'];
if (json_last_error() !== JSON_ERROR_NONE) {
echo 'Invalid JSON: ' . json_last_error_msg();
}
JSON_UNESCAPED_UNICODE keeps Hindi/Punjabi text readable instead of \u codes.
JSON in JavaScript
const text = '{"from":"Ludhiana","fare":3700}';
const obj = JSON.parse(text); // text → object
console.log(obj.fare);
const back = JSON.stringify(obj); // object → text
const pretty = JSON.stringify(obj, null, 2);
With fetch, await res.json() parses the response for you. See fetch tutorial.
Where you'll see JSON
- REST APIs. See REST API explained
- Schema markup (JSON-LD) for SEO, such as LocalBusiness and FAQ schema
composer.jsonandpackage.jsonconfig files- Exports from tools and analytics
FAQs
Is JSON better than XML?
For web APIs it's usually simpler and smaller. XML is still used in sitemaps and some older systems.
Can JSON store dates?
Not as a special type. Use strings in ISO format, like "2026-10-29".
Was this article helpful?
Your answer helps us improve our guides