Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

What is JSON? and What is the Use in Programming?

What is JSON, and what is the point in programming?
For those who have just heard JSON, you might ask:
"What is JSON? and what is the point in programming? then how to use it? "
Me too.
At first I did not understand, what was JSON and how it behaved in the world of programming.
But after going through various kinds of experiments, finally I could understand.

Whatever programming language you will use ...
... you must understand JSON.
Because JSON is widely used in creating applications and is used in almost all programming languages.
On this occasion, we will discuss the basics of JSON that you should know.
Just know these 5 things:

What is JSON?

We start from the understanding of JSON first ...
JSON (JavaScript Object Notation) is a data format used for exchanging and storing data . 1
Keywords to remember: "data exchange & storage"
JSON is a part (subset) of Javascript. JSON can be read with various programming languages ​​such as C, C ++, C #, Java, Javascript Perl, Python, and more.
This makes JSON the ideal language for data interrelation between applications .
JSON even dominates its XML predecessor (eXtensible Markup Language) ...
When compared to XML, JSON is simpler and easier to read.
Comparison of JSON with XML
Therefore, more people use JSON than XML.
The following is a graph of JSON usage compared to XML, YAML, CSV, and protocol-buffers.
JSON usage charts in the World

A Short History of JSON

JSON was first popularized by Douglas Crockford. software engineer who is also involved in developing Javascript programming languages. 2
Doglas Crockford
Doglas Crockford
JSON is not found by just one person. It used to be called JSON ...
This means that the word "JSON" does not yet exist. People only know Javascript Objects that are sent over the network.
Since the explosion of AJAX technology in 2000, JSON was introduced and in 2001, the json.org domain began broadcasting.
Until now, JSON is widely used everywhere.

Application of JSON in Programming

JSON is usually used as a standard format for exchanging data between applications.
Exchange data between applications with JSON
But actually not only that, there are still other functions from JSON.
Here are some of the implementations of JSON that I have met:

JSON Basic Structure

Look at this JSON structure.
JSON basic structure
This is the simplest structure ...
JSON always starts with curly brackets { and closes with parentheses }.
Then inside curly braces, it contains data that is the format key and value . If there is more than one data, then separated by commas and in the last data is not given a comma.
JSON format object
Then the key and valude are separated by a colon.
Oh yeah, for value ...
We can provide any data type. We can even fill with arrays and objects.
The following are the data types supported by JSON.
Data types supported by JSON
Well for arrays, it is made with square brackets [...].
Array on JSON
Examples like this:
{
    "name": "Dian",
    "hobbies": ["Coding", "Blogging", "Drawing"]
}
... and for example objects like this:
{
    "name": "petanikode",
    "url": "https://www.petanikdoe.com",
    "rank": 1,
    "socialmedia": {
        "facebook": "petanikode",
        "twitter": "petanikode",
        "instagram": "petanikode",
        "youtube": "petanikode",
        "github": "petanikode"
    }
}
Notice in the key socialmediathere we give the value with the object.

How to produce and consume JSON data

Each programming language has different ways to produce (create) and consume JSON data.
In Javascript, we can use functions JSON.stringify() to create JSON from Javascript objects.
Example:
// objek javascript
var person = {
    name: "Dian",
    age: 23
}

// string JSON
var jsonString = JSON.stringify(person);

// maka akan menghasilkan:
// {"name":"Dian","age":23}
In Java, we can use the Gson library to serialize (create) and decentralize JSON data .
JSON serialization and decentralization
In the Python programming language , we can use modules jsonto create and read (consume) JSON data.
Example:
>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print json.dumps("\"foo\bar")
"\"foo\bar"
>>> print json.dumps(u'\u1234')
"\u1234"
>>> print json.dumps('\\')
"\\"
>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
{"a": 0, "b": 0, "c": 0}
>>> from StringIO import StringIO
>>> io = StringIO()
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'
Another example:
import json

# dictionary
person = {
    "name": "Dian",
    "age": 23
}

# Membuat JSON dari dictionary
json.dumps(person)

# Maka akan menghasilkan:
# '{"age": 23, "name": "Dian"}'
In the PHP programming language, we can use functions json_encode()to generate JSON from associative arrays and functions json_decode() to convert JSON to Array.
Example:
<?php

// data dengan array assosiatif
$person = [
    "name" => "Dian",
    "age" => 23
];

// membuat JSON dari array
echo json_encode($person);

// maka akan menghasilkan:
// {"name":"Dian","age":23}
?>
In essence, each programming language has its own functions, modules, and libraries to create and read JSON data.
Here is a list of tutorials that you can read:
  1. How to Serialize and Deseralize JSON data in Java using GSON
  2. How to Parse JSON data in Python
  3. How to Parse JSON data in PHP
  4. How to Parse JSON data in Javascript.


0 Komentar untuk "What is JSON? and What is the Use in Programming?"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top