JSON: Syntax dan How To

March 12, 2016

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays


Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an array of objects and assign data to it, like this:

var employees = [
{“firstName”:”John”, “lastName”:”Doe”},
{“firstName”:”Anna”, “lastName”:”Smith”},
{“firstName”:”Peter”,”lastName”: “Jones”}

Bisa diakses dengan cara:

employees[0].firstName + ” “ + employees[0].lastName; // returns John Doe

employees[0][“firstName”] + ” “ + employees[0][“lastName”];

Modifikasi bisa dilakukan dengan mengakses sbb:

employees[0].firstName = “Gilbert”;

employees[“firstName”].firstName = “Gilbert”;

Contoh how to:

<!DOCTYPE html>

<h2>Create Object from JSON String</h2>

<p id=”demo”></p>

var text = ‘{“employees”:[‘ +
‘{“firstName”:”John”,”lastName”:”Doe” },’ +
‘{“firstName”:”Anna”,”lastName”:”Smith” },’ +
‘{“firstName”:”Peter”,”lastName”:”Jones” }]}’;

obj = JSON.parse(text);
document.getElementById(“demo”).innerHTML =
obj.employees[1].firstName + ” ” + obj.employees[1].lastName;



jangan lupa. kuncinya sebetulnya di : document.getElementById(“demo”).innerHTML =

Gampang kan?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: