Format JSON — API

JSON (JavaScript Object Notation) adalah format data standar untuk API modern. Syntax JSON: { "name": "Budi", "age": 25, "active": true, "address": null, "hobbi

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:

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

Yang akan kamu pelajari