Apa itu JSON..?

Selamat pagi kawan…

Kemarin-kemarin ada orang Finance nanya cara convert data Excel ke JSON, lha wong saya ndak tau mbak, apaan tuh JSON..? wakakakaka

Setelah dicari tau ternya JSON  kepanjangan dari JavaScript Object Notation (bahasa Indonesia: notasi objek JavaScript), adalah suatu format ringkas pertukaran data komputer. Formatnya berbasis teks dan terbaca-manusia serta digunakan untuk merepresentasikan struktur data sederhana dan larik asosiatif (disebut objek). Format JSON sering digunakan untuk mentransmisikan data terstruktur melalui suatu koneksi jaringan pada suatu proses yang disebut serialisasi. Aplikasi utamanya adalah pada pemrograman aplikasi web AJAX dengan berperan sebagai alternatif terhadap penggunaan tradisional format XML.

Walaupun JSON didasarkan pada subset bahasa pemrograman JavaScript (secara spesifik, edisi ketiga standar ECMA-262, Desember 1999) dan umumnya digunakan dengan bahasa tersebut, JSON dianggap sebagai format data yang tak tergantung pada suatu bahasa. Kode untuk pengolahan dan pembuatan data JSON telah tersedia untuk banyak jenis bahasa pemrograman. Situs json.org menyediakan daftar komprehensif pengikatan JSON yang tersedia, disusun menurut bahasa.

Format JSON dispesifikasikan di RFC 4627 oleh DouglasCrockford. Tipe media Internet resmi JSON adalah application/json sedangkan ekstensi berkasnya adalah.json.

Pada Desember 2005, Yahoo! mulai menawarkan opsi format JSON untuk layanan webnya. Google mulai menyediakan umpan JSON untuk protokol web GData-nya pada Desember 2006.

Contoh sintaks:

{
     "namaDepan": "Budi",
     "namaBelakang": "Subudi",
     "alamat": {
         "namaJalan": "Jl. Sudirman 15A",
         "kota": "Jakarta Selatan",
         "provinsi": "DKI Jakarta",
         "kodePos": 11111
     },
     "nomerTelepon": [
         "021 555-1234",
         "021 555-4567"
     ]
 }

Ada juga contoh script HTMLnya sbg berikut:
<!DOCTYPE html>
<!–
* csv-to-json: A utility that converts data format from CSV to JSON.
* Copyright (C) 2009-2012 Christopher Parker <http://www.cparker15.com/&gt;
*
* csv-to-json is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* csv-to-json is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with csv-to-json. If not, see <http://www.gnu.org/licenses/&gt;.
–>
<html lang=”en”>
<head>
<meta charset=”utf-8″ />
<title>CSV to JSON</title>
<style>
.error { font-weight: bold; color: #F00; }
</style>
<script src=”json/json2.js”> </script>
<script>
var csvRows = []; var objArr = [];
var benchmarkStart, benchmarkParseEnd, benchmarkObjEnd, benchmarkJsonEnd, benchmarkPopulateEnd;
function setMessage (message, error)
{
document.getElementById(“message”).innerHTML = ‘<p>’ + message + ‘</p>’;
if (error)
document.getElementById(“message”).className = “error”;
else
document.getElementById(“message”).className = “”;
}
function parseCSVLine (line)
{
line = line.split(‘,’);
// check for splits performed inside quoted strings and correct if needed
for (var i = 0; i < line.length; i++)
{
var chunk = line[i].replace(/^[\s]*|[\s]*$/g, “”);
var quote = “”;
if (chunk.charAt(0) == ‘”‘ || chunk.charAt(0) == “‘”) quote = chunk.charAt(0);
if (quote != “” && chunk.charAt(chunk.length – 1) == quote) quote = “”;
if (quote != “”)
{
var j = i + 1;
if (j < line.length) chunk = line[j].replace(/^[\s]*|[\s]*$/g, “”);
while (j < line.length && chunk.charAt(chunk.length – 1) != quote)
{
line[i] += ‘,’ + line[j];
line.splice(j, 1);
chunk = line[j].replace(/[\s]*$/g, “”);
}
if (j < line.length)
{
line[i] += ‘,’ + line[j];
line.splice(j, 1);
}
}
}
for (var i = 0; i < line.length; i++)
{
// remove leading/trailing whitespace
line[i] = line[i].replace(/^[\s]*|[\s]*$/g, “”);
// remove leading/trailing quotes
if (line[i].charAt(0) == ‘”‘) line[i] = line[i].replace(/^”|”$/g, “”);
else if (line[i].charAt(0) == “‘”) line[i] = line[i].replace(/^’|’$/g, “”);
}
return line;
}
function csvToJson ()
{
var message = “”;
var error = false;
var f = document.forms["convertForm"];
var csvText = f.elements["csv"].value;
var jsonText = “”;
setMessage(message, error);
if (csvText == “”) { error = true; message = “Enter CSV text below.”; }
if (!error)
{
benchmarkStart = new Date();
csvRows = csvText.split(/[\r\n]/g); // split into rows
// get rid of empty rows
for (var i = 0; i < csvRows.length; i++)
{
if (csvRows[i].replace(/^[\s]*|[\s]*$/g, ”) == “”)
{
csvRows.splice(i, 1);
i–;
}
}
if (csvRows.length < 2) { error = true; message = “The CSV text MUST have a header row!”; }
else
{
objArr = [];
for (var i = 0; i < csvRows.length; i++)
{
csvRows[i] = parseCSVLine(csvRows[i]);
}
benchmarkParseEnd = new Date();
for (var i = 1; i < csvRows.length; i++)
{
if (csvRows[i].length > 0) objArr.push({});
for (var j = 0; j < csvRows[i].length; j++)
{
objArr[i - 1][csvRows[0][j]] = csvRows[i][j];
}
}
benchmarkObjEnd = new Date();
jsonText = JSON.stringify(objArr, null, “\t”);
benchmarkJsonEnd = new Date();
f.elements["json"].value = jsonText;
benchmarkPopulateEnd = new Date();
message = getBenchmarkResults();
}
}
setMessage(message, error);
}
function getBenchmarkResults ()
{
var message = “”;
var totalTime = benchmarkPopulateEnd.getTime() – benchmarkStart.getTime();
var timeDiff = (benchmarkParseEnd.getTime() – benchmarkStart.getTime()); var mostTime = “parsing CSV text”;
if ((benchmarkObjEnd.getTime() – benchmarkParseEnd.getTime()) > timeDiff) { timeDiff = (benchmarkObjEnd.getTime() – benchmarkParseEnd.getTime()); mostTime = “converting to objects”; }
if ((benchmarkJsonEnd.getTime() – benchmarkObjEnd.getTime()) > timeDiff) { timeDiff = (benchmarkJsonEnd.getTime() – benchmarkObjEnd.getTime()); mostTime = “building JSON text”; }
if ((benchmarkPopulateEnd.getTime() – benchmarkJsonEnd.getTime()) > timeDiff) { timeDiff = (benchmarkPopulateEnd.getTime() – benchmarkJsonEnd.getTime()); mostTime = “populating JSON text”; }
message += csvRows.length + ” CSV line” + (csvRows.length > 1 ? ‘s’ : “”) + ” converted into ” + objArr.length + ” object” + (objArr.length > 1 ? ‘s’ : “”) + ” in ” + (totalTime / 1000) + ” seconds, with an average of ” + ((totalTime / 1000) / csvRows.length) + ” seconds per object. Most of the time was spent on ” + mostTime + “, which took ” + (timeDiff / 1000) + ” seconds.”;
return message;
}
</script>
</head>
<body>
<p>Notes:</p>
<ul>
<li>The <acronym title=”comma-separated values”>CSV</acronym> text <strong style=”text-transform: uppercase; “>must</strong> have a header row.</li>
<li>This utility does not currently check for escaped quotes inside of like quotes (e.g.: <code>”foo, \”bar\” baz”</code>).</li>
</ul>
<hr noshade=”noshade” />
<div id=”message”><p></p></div>
<form id=”convertForm” name=”convertForm” onsubmit=”return false; “>
<label for=”csv”><acronym title=”comma-separated values”>CSV</acronym>:</label><br /><textarea id=”csv” name=”csv” rows=”10″ cols=”60″></textarea><br /><br />
<label for=”json”><acronym title=”JavaScript Object Notation”>JSON</acronym>:</label><br /><textarea id=”json” name=”json” rows=”10″ cols=”60″ readonly=”readonly”></textarea><br /><br />
<input type=”button” value=”Convert” onclick=”csvToJson(); ” /> <input type=”reset” />
</form>
</body>
</html>


About these ads

3 thoughts on “Apa itu JSON..?

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