JSON (JavaScript Object Notation) adalah format data standar untuk API modern.
Syntax JSON:
{
"name": "Budi",
"age": 25,
"active": true,
"address": null,
"hobbies": ["coding", "gaming"],
"contact": {
"email": "[email protected]",
"phone": "08123456789"
}
}
Tipe data JSON:
| Tipe | Contoh |
|---|---|
| String | "hello" (harus double quotes!) |
| Number | 42, 3.14 |
| Boolean | true, false |
| Null | null |
| Array | [1, 2, 3] |
| Object | {"key": "value"} |
Aturan JSON:
- Key HARUS pakai double quotes
"key" - Tidak ada trailing comma
- Tidak ada komentar
- Tidak ada
undefined(gunakannull)
Content-Type header:
Content-Type: application/json
Header ini memberitahu server/client bahwa body berisi JSON.
Parsing JSON (di JavaScript):
const json = '{"name": "Budi"}';
const obj = JSON.parse(json); // String → Object
const str = JSON.stringify(obj); // Object → String