Introduction to JSON
Login
JSON (JavaScript Object Notation) is a format used for data sharing.
As its name suggests, its origin is JavaScript, but nowadays it is also supported by many languages such as Python, Ruby, PHP, Java.
JSON has a readable, lightweight format and requires much less formatting than XML.
In this guide, we will explain what data can be used in JSON files,
We will also discuss the general structure and syntax of this format.
Understanding Syntax and Structure
JSON files are usually saved with the .json extension.
But it can also be used in other formats (e.g. .html):
- As JSON string in quotes,
- or as a JSON object assigned to a variable.
This format is used specifically for data transmission between the web server and the client (browser).
A JSON object stores data in the format key : value
and is usually written within curly brackets {}.
JSON objects mostly appear in .json files,
but it can also exist within a program as a JSON object or string.
Here is an example of a simple JSON object:
{
"ad" : "Ali",
"soyad" : "Keskin",
"ulke" : "Türkiye",
"online" : true,
"followers" : 456
}
While this is just a quick example, JSON can actually take dozens of lines.
But the basic logic is always the same: The data structure starts and ends with two curly brackets { },
In the middle there are key: value) pairs.
The majority of data used in JSON is typically kept in a JSON object.
In JSON, there is a colon : between key and value.
Each key-value pair is separated by a comma ( , )**.
The example structure looks like this:
"ad" : "Ali",
In JSON, keys are located to the left of the colon :.
Each key must be written in double quotes, for example "anahtar".
Keys can be valid strings but must be unique within each object.
Spaces can be used in key names (such as "ad soyad"),
But in this case, access becomes difficult while programming.
That's why it's usually more practical to use an underscore: "ad_soyad".
values in JSON are located to the right of the colon :.
At a basic level, a JSON value can be one of six data types:
- String (text)
- Number (number)
- Object (object)
- Array (array)
- Boolean (
trueorfalse) - null (null value)
More broadly, JSON values are not just simple types;
It can also consist of more complex structures such as JSON object or array.
We will see these in detail in the next section.
Each data type maintains its own syntax:
- String values are written in quotes
"örnek", - Number values are written directly
25.
In .json files, data is often spread over multiple lines.
But the same content can also be written in a single line. For example:
{"ad":"Ali","soyad":"Keskin","yas":25}
JSON written on a single line is usually stored in other file types.
or we see it as a JSON string.
Spreading JSON across multiple lines makes it much more readable, especially when working with large data sets.
Because JSON does not take into account spaces between elements,
colon : and separating key-value pairs with spaces
You can make the data more understandable for people.
{
"ad" : "Ali",
"soyad" : "Keskin",
"online" : true
}
It is important to remember that JSON objects are similar in appearance to JavaScript objects,
but they are not the same thing.
- You can define and use functions in JavaScript objects.
- But in JSON functions cannot be used as values.
The most important feature of JSON is that it keeps the data in such a format that
It is easily portable between different programming languages and works compatible with all of them.
In contrast, JavaScript objects can only be used within the JavaScript language.
JSON structures can sometimes get quite complex.
The reason for this is that it can contain nested objects and arrays.
You'll learn more about these more complex JSON structures in the next section.
Working with Complex Structures in JSON
JSON contains not only simple data but also nested objects
and can also store arrays.
These objects and arrays, like any other, are kept as values assigned to keys
and they can also contain key-value pairs.
Nested Objects
In the users.json file below, each user ("ahmet", "ayşe", "mehmet", "zeynep")
There is a nested JSON object for .
These objects contain "username" and "location" information for each user.
Each user input is actually a nested JSON object instance:
{
"ahmet": {
"username": "ahmet",
"location": "İstanbul"
},
"ayse": {
"username": "ayse",
"location": "Ankara"
},
"mehmet": {
"username": "mehmet",
"location": "İzmir"
},
"zeynep": {
"username": "zeynep",
"location": "Bursa"
}
}
In this example using curly brackets { } for each user
Created nested JSON objects containing username and location information.
As in other JSON structures, elements are separated by commas ( , ).
Nested Arrays
Data in JSON format can also be nested in array structures.
Arrays are written inside square brackets [ ], creating an ordered collection
and can accommodate different data types together.
For example, if a user has different websites or social media profiles,
It makes sense to group them into a single array.
Below is a simple nested array example for a user named "ahmet":
{
"first_name" : "Ahmet",
"last_name" : "Yılmaz",
"location" : "İstanbul",
"websites" : [
{
"description" : "kişisel site",
"URL" : "https://ahmetyilmaz.com"
},
{
"description" : "blog",
"URL" : "https://ahmetyilmaz.com/blog"
}
],
"social_media" : [
{
"description" : "twitter",
"link" : "https://twitter.com/ahmetyilmaz"
},
{
"description" : "facebook",
"link" : "https://facebook.com/ahmetyilmaz"
},
{
"description" : "github",
"link" : "https://github.com/ahmetyilmaz"
}
]
}
The "websites" and "social_media" keys here belong to Ahmet.
It uses the array structure to store two websites and three social media profiles.
You can understand that these are series from the square brackets [ ].
Using nested structures in JSON allows you to handle more complex and hierarchical data
It allows you to work more regularly.
JSON and XML Comparison
XML (eXtensible Markup Language), readable by both humans and machines
It is one of the data storage methods and can be used in many programming languages.
In fact, XML is similar to JSON in many ways, but:
- It requires much more typing, meaning it takes longer to read and write.
- XML files must be parsed with a special XML parser,
JSON, on the other hand, can be easily processed with a standard function. - While JSON supports arrays, XML does not support arrays.
Below you can see a simple XML format example:
<kullanicilar>
<kullanici>
<kullanici_adi>Ahmet</kullanici_adi>
<konum>Marmara Denizi</konum>
</kullanici>
<kullanici>
<kullanici_adi>Ayse</kullanici_adi>
<konum>Ege Denizi</konum>
</kullanici>
<kullanici>
<kullanici_adi>Mehmet</kullanici_adi>
<konum>Karadeniz</konum>
</kullanici>
<kullanici>
<kullanici_adi>Zeynep</kullanici_adi>
<konum>Akdeniz</konum>
</kullanici>
</kullanicilar>
Now let's compare the same data in JSON format:
{
"kullanicilar": [
{
"kullanici_adi": "Ahmet",
"konum": "Marmara Denizi"
},
{
"kullanici_adi": "Ayse",
"konum": "Ege Denizi"
},
{
"kullanici_adi": "Mehmet",
"konum": "Karadeniz"
},
{
"kullanici_adi": "Zeynep",
"konum": "Akdeniz"
}
]
}
JSON has a much more compact structure than XML and does not require end tags.
Also, as you can see in the JSON example, while arrays can be used, this is not possible in XML.
(recognized by square brackets [ ]).
If you are familiar with HTML, you may have noticed that XML's tag structure is similar to HTML.
JSON, on the other hand, is lighter, less verbose and faster, especially in scenarios such as AJAX applications.
But before deciding which format to use, you should definitely consider the needs of the project.
Result
JSON is a lightweight format used to share, store and manipulate data.
Today, it is supported in almost all popular languages and is especially widely preferred in APIs (e.g. Twitter API).
Its natural compatibility with JavaScript also makes it more useful.
Generally, you will obtain your own .json files from other sources rather than writing them from scratch.
That's why you should focus on how you can use it in your programs rather than the structure of JSON.
For example:
- Mr. to convert CSV or tabular data to JSON. You can use open source tools like Data Converter.
- For XML ↔ JSON conversion utilities-online.info can do the job.
- JSONLint to check if JSON is correct or not
To test it on the web, you can use tools such as JSFiddle.
In short, JSON is a simple but powerful standard for anyone working with data.

