🌙
Happy Karwa Chauth! Wishing all couples a blessed Karwa Chauth. May your bond grow stronger every year. From Team Job...
💬 Share
Web Development Tutorials

JSON Explained for Beginners: Syntax, Examples and Using It in PHP and JavaScript

JSON is the data format behind APIs, AJAX and settings files. Here's JSON explained with simple examples in PHP and JavaScript.

In this article · 7 sections
  1. What JSON looks like
  2. Syntax rules
  3. Common errors
  4. JSON in PHP
  5. JSON in JavaScript
  6. Where you'll see JSON
  7. FAQs
  8. Is JSON better than XML?
  9. Can JSON store dates?

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.json and package.json config 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

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬