What is JSON? How to Use?

JSON (JavaScript Object Notation) is a text-based data package object  and can contain various types of data that are used very often in data exchange and conversion in programing. Because of being a fast tool with its simple structure, json objects are often preferred in programming. It is important to know what it is and how it is used, as it is used in almost any programming language. We can compare JSON packages to a dictionary structure in general. There are two types of uses in a JSON package. In the first use, there is simply key, value (value) system. And Json is used more in that way. An example of this usage json data package is as follows:

"name": "Bill",
"surname": "Wayne",
"age": 21,
"city": "York",
"job": null

As can be seen in the example above, for example, "name" is key, "Bill" is its value.

There is no key-value structure in other json usage. It is similar to array. As an example of this use:

[ "apple", "pear", "strawberry" ]

Json data packages are especially useful for processing and editing the data received from the database in a coding environment in a practical way. The json structure is designed for javascript syntax, but it is a platform independent format so it can be used with almost any programming language. By using json objects with programming languages, you can easily process  data and performnecessary transformations and also perform your database operations with json objects.

Basic data types used in Json packages can be expressed as "number, string, boolean, array, object, null".

Number: Data in the integer or float type can be used in the json object.

String: Express textual data types

Boolean: True, False or 0,1 is boolean type. It should be used in this way when necessary, in order to take up little space in memory.

Array: It is used to hold multiple data in one key.

Null: It is used for keys without a value. In the example above, null is used because there is no value in the profession key.

JSON Examples

In this section, we will give examples of using json in various programming languages.

var studentSt = '{"point":46, "isPassed":true}';
var obj = JSON.parse(studentSt);
console.log(obj.point); // prints value of the point variable in json object: 42 console.log(obj.isPassed); // prints value of the isPassed variable in json object: true

This code gives an example of using the most basic json in javascript. It allows us to divide the json object into a variable by parse() function. So we can reach every component of the json object as in the example above when we need it.


$studentSt = '{"point":46, "isPassed":true}';
print $obj->{'point'}; // prints 46 print $obj->{'isPassed'}; //prints boolean 1 ?>

In this json example, a variable defined directly as a json object in Php. And json object is transferred to another variable with the json_decode() function for using its components directly.


import json
studentSt =   '{"point": 23, "isPassed": false}'
obj = json.loads(studentSt)
print(obj) #Prints: {'point': 23, 'isPassed': False}

In this Python code, we first included the json library and created a json object with the name "studentSt". Then json data was converted to the dictionary type variable in Python with json.loads() function and printed.


As can be seen from the examples above,  basic logic in using json objects is similar in all programming languages. In other words, the json object is created and received first, then it is transformed with certain functions to be used in the program code and used through variables. The basic logic in using Json is to exchange data.