Apa itu JSON? Panduan Lengkap untuk Pemula
JSON (JavaScript Object Notation) adalah format pertukaran data berbasis teks yang ringan, mudah dibaca manusia, dan mudah diproses oleh mesin. Hampir semua API web modern — dari REST API e-commerce sampai webhook pembayaran — menggunakan JSON sebagai format data utamanya.
Struktur Dasar JSON
JSON dibangun dari dua struktur utama: object (kumpulan pasangan key-value, ditulis dengan kurung kurawal {}) dan array (daftar nilai berurutan, ditulis dengan kurung siku []). Berikut contoh sederhana:
{
"nama": "Budi Santoso",
"umur": 28,
"aktif": true,
"hobi": ["coding", "membaca", "futsal"],
"alamat": {
"kota": "Jakarta",
"kodePos": "12345"
}
}
Tipe Data yang Didukung JSON
- String — teks, selalu diapit tanda kutip dua
"..." - Number — angka, bisa bulat atau desimal, tanpa tanda kutip
- Boolean —
trueataufalse - Null — nilai kosong/tidak ada
- Object — kumpulan key-value bersarang
- Array — daftar nilai, bisa campur tipe
Kenapa JSON Begitu Populer?
Tiga alasan utama: pertama, sintaksnya ringkas dan mudah dibaca dibanding XML. Kedua, hampir semua bahasa pemrograman punya parser JSON built-in (JavaScript, Python, PHP, Java, Go, dll). Ketiga, JSON native cocok dengan JavaScript karena strukturnya identik dengan object literal JS, sehingga sangat efisien dipakai di aplikasi web dan mobile.
Kesalahan Umum saat Menulis JSON
- Lupa tanda kutip dua pada key — JSON wajib
"key", bukankeytanpa kutip seperti di JavaScript object biasa. - Trailing comma — koma ekstra setelah elemen terakhir dalam array/object akan membuat JSON invalid.
- Comment tidak didukung — berbeda dengan JS, JSON murni tidak punya sintaks komentar
//atau/* */. - Single quote — JSON hanya menerima tanda kutip dua, tidak menerima
'single quote'.
Kapan Sebaiknya Pakai JSON?
JSON paling cocok untuk: response API, file konfigurasi aplikasi web/mobile, penyimpanan data NoSQL (seperti MongoDB), dan pertukaran data antar microservice. Untuk file konfigurasi yang butuh komentar dan lebih "manusiawi" dibaca, YAML biasanya jadi pilihan lebih nyaman — bisa kamu baca lebih lanjut di artikel JSON vs YAML: Perbedaan dan Kapan Memakainya.
🔧 Coba Format & Validasi JSON Kamu di JSONYAMifyWhat is JSON? A Complete Beginner's Guide
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that's easy for humans to read and easy for machines to parse. Nearly every modern web API — from e-commerce REST APIs to payment webhooks — uses JSON as its primary data format.
Basic JSON Structure
JSON is built from two core structures: an object (a set of key-value pairs wrapped in curly braces {}) and an array (an ordered list of values wrapped in square brackets []). Example:
{
"name": "John Doe",
"age": 28,
"active": true,
"hobbies": ["coding", "reading", "futsal"],
"address": {
"city": "Jakarta",
"zip": "12345"
}
}
Data Types Supported by JSON
- String — text, always wrapped in double quotes
"..." - Number — integer or decimal, no quotes
- Boolean —
trueorfalse - Null — represents an empty/absent value
- Object — nested key-value pairs
- Array — a list of values, possibly mixed types
Why is JSON So Popular?
Three main reasons: first, its syntax is concise and more readable than XML. Second, nearly every programming language ships with a built-in JSON parser (JavaScript, Python, PHP, Java, Go, etc). Third, JSON maps natively onto JavaScript object literals, making it extremely efficient for web and mobile apps.
Common JSON Mistakes
- Missing double quotes on keys — JSON requires
"key", not a barekeylike a plain JS object. - Trailing commas — an extra comma after the last element in an array/object makes the JSON invalid.
- Comments aren't supported — unlike JS, pure JSON has no
//or/* */comment syntax. - Single quotes — JSON only accepts double quotes, never
'single quotes'.
When Should You Use JSON?
JSON is ideal for API responses, web/mobile app config files, NoSQL data storage (like MongoDB), and data exchange between microservices. For config files that need comments and a more human-friendly format, YAML is often the more comfortable choice — read more in JSON vs YAML: Differences and When to Use Each.
🔧 Format & Validate Your JSON on JSONYAMify