json cheatsheet, json tutorial, json syntax, json examples, json parser, json objects and arrays, json format rules, json vs xml, json in javascript, json in php, json api response
json cheatsheet, json tutorial, json syntax, json examples, json parser, json objects and arrays, json format rules, json vs xml, json in javascript, json in php, json api response

JSON Cheatsheet — The Ultimate Developer Reference

JSON (JavaScript Object Notation) is a lightweight and language-independent data-interchange format widely used in web development, APIs, and configuration files.
It allows systems to exchange structured data in a readable format, making it a universal data language across platforms.

This JSON Cheatsheet gives you a complete reference — from syntax basics to real-world examples — for developers working with JavaScript, Python, PHP, Node.js, and APIs.


What is JSON?

JSON is a text-based format for representing structured data. It uses simple syntax with key-value pairs, arrays, and nested objects.

Key Benefits:

  • Easy to read and write
  • Lightweight and language-independent
  • Compatible with most modern APIs
  • Easily parsed into native data structures

Example of JSON:

{
  "name": "John Doe",
  "age": 30,
  "skills": ["HTML", "CSS", "JavaScript"],
  "active": true
}

JSON Syntax Rules

ElementDescriptionExample
DataName/value pairs"name": "Alice"
Objects{} contain key/value pairs{"city": "New York"}
Arrays[] contain ordered values["red", "green", "blue"]
KeysMust be strings, enclosed in double quotes"language"
ValuesCan be string, number, boolean, array, object, or null"PHP", 123, true
WhitespaceIgnored by parserOptional
CommaUsed to separate key/value pairs"a": 1, "b": 2
json cheatsheet, json tutorial, json syntax, json examples, json parser, json objects and arrays, json format rules, json vs xml, json in javascript, json in php, json api response

JSON Data Types

TypeExample
String"language": "Python"
Number"year": 2025
Boolean"active": true
Array"colors": ["blue", "green", "red"]
Object"user": {"id": 1, "name": "John"}
Null"address": null

JSON Objects and Arrays

Object Example:

{
  "id": 101,
  "title": "Developer",
  "salary": 75000
}

Array Example:

[
  "JavaScript",
  "Python",
  "PHP"
]

Array of Objects:

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" }
]

Nested JSON Example

{
  "student": {
    "name": "Sara",
    "grade": "A",
    "subjects": ["Math", "Science", "English"],
    "address": {
      "city": "New Delhi",
      "country": "India"
    }
  }
}

Working with JSON in JavaScript

Parsing JSON

Convert JSON string → JavaScript object:

const jsonData = '{"name": "John", "age": 25}';
const obj = JSON.parse(jsonData);
console.log(obj.name); // John

Stringify JSON

Convert JavaScript object → JSON string:

const user = { name: "Alice", age: 22 };
const jsonString = JSON.stringify(user);
console.log(jsonString);

Working with JSON in PHP

<?php
$json = '{"name":"Alice","age":22}';
$data = json_decode($json, true);  // Convert to PHP array
echo $data['name']; // Alice

$user = array("name"=>"John", "age"=>30);
echo json_encode($user);
?>

Working with JSON in Python

import json

data = '{"name": "John", "age": 30}'
parsed = json.loads(data)
print(parsed['name'])

person = {"name": "Alice", "city": "London"}
json_string = json.dumps(person)
print(json_string)

JSON in REST APIs

A typical API response in JSON:

{
  "status": "success",
  "data": {
    "id": 101,
    "title": "Learn JSON",
    "category": "Programming"
  }
}

JSON Formatting Tips

✅ Always use double quotes for keys and strings
✅ Avoid trailing commas
✅ Use online tools like phponline.in/json-formatter for clean formatting
✅ Validate JSON before using it in production


JSON vs XML

FeatureJSONXML
ReadabilityEasierVerbose
SyntaxKey-valueTags
Data SizeSmallerLarger
Parsing SpeedFasterSlower
Browser SupportNative in JSRequires parser
Data TypesSupports multipleAll as string

JSON Errors & Validation

Common issues:

  • Missing double quotes
  • Extra commas
  • Unescaped characters

You can validate JSON using:

npm install -g jsonlint
jsonlint file.json

JSON Pretty Print

Format JSON for readability:

JSON.stringify(data, null, 2);

Best Practices for JSON

✅ Use lowercase keys for consistency
✅ Avoid deeply nested objects (affects performance)
✅ Keep structure consistent across APIs
✅ Include metadata like status, message, or timestamp



FAQ — JSON Cheatsheet

Q1: What is JSON used for?
JSON is used for data exchange between a client and server in web applications and APIs.

Q2: How is JSON different from JavaScript objects?
JSON is a text format, while JavaScript objects are in-memory structures.

Q3: Can JSON contain comments?
No, JSON doesn’t support comments — use metadata instead.

Q4: What tools can validate JSON?
You can use JSONLint, Postman, or online validators like json-validator.

Q5: How do I convert JSON to CSV?
You can use Python’s pandas library or online converters to transform JSON data into CSV.

Related Article
Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

HTML5 Cheat Sheet — Complete Tag Reference & Examples

HTML5 is the core markup language of the modern web, used to structure content such as text, images, forms, and Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments