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 ...
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 .
When compared to XML, JSON is simpler and easier to read.
Therefore, more people use JSON than XML.
The following is a graph of JSON usage compared to XML, YAML, CSV, and protocol-buffers.
A Short History of JSON
JSON was first popularized by Douglas Crockford. A software engineer who is also involved in developing Javascript programming languages. 2
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.
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 as a format for exchanging client and server data or between applications. Example: RESTful API ;
- JSON as a place to store data, for example: Mongodb database ;
- JSON is used to save the project configuration, for example: the composer.json file in the PHP and package.json projects on Nodejs ;
- JSON is used to store data configuration and storage in Hugo;
- JSON is used to save the project configuration at Nodejs ;
- JSON is used to store data menifest;
- and much more.
JSON Basic Structure
Look at this JSON 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.
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.
Well for arrays, it is made with square brackets
[...]
.
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
socialmedia
there 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}
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:
- How to Serialize and Deseralize JSON data in Java using GSON
- How to Parse JSON data in Python
- How to Parse JSON data in PHP
- How to Parse JSON data in Javascript.
Reference: https://www.petanikode.com/json-pemula/
0 Komentar untuk "What is JSON? and What is the Use in Programming?"
Silahkan berkomentar sesuai artikel