Welcome to Codingcompiler Blog. In this tutorial, you will learn about what is JSON and an introduction to JSON for beginners. Let’s start learning about JSON or JavaScript Object Notation.
What is JSON?
JSON or JavaScript Object Notation is a text-based data interchange format based on JavaScript. However, it can be used in any programming language. The format was designed by Douglas Crockford.
JSON is used in the REST API. You can also use XML as an alternative, but developers prefer JSON more, as it is more readable and weighs less.
How JSON works
The following can be used as values in JSON:
- numbers;
- lines;
- arrays;
- JSON objects;
- literals (boolean values true, false, and null).
With simple values, there will be no difficulty. Let’s analyze arrays and JSON objects, because, in fact, we will have to work with them.
JSON object
A JSON object is an unordered set of pairs “key:value” enclosed in curly braces { } and interacting with it is like a dictionary.
The key is the name of the parameter (property) that we pass to the server. It serves as a marker for the system receiving the request so that it understands what we have sent to it.
Let’s look at a simple example:
{ "name": "John", "age": 26 }
We passed a value “John” with a key “name”, in order to get this property, you need to access it by its key, without which it would be impossible to do this.
The value can be more than just a string or a number. It could be something else! Or an array, or an object within an array, an array within an object… Absolutely any number of nesting levels!
If we are talking about parameters, then only strings are placed in quotes, everything else is not.
Line breaks are optional. In general, spaces and hyphens are needed only for human readability, the system will understand without them.
For example, we can write a JSON object in this way, and this will also be correct:
{"name": "John","age": 26}
The key is ALWAYS a string, and we quote it anyway.
The keys can be written in any order because a JSON object is an unordered set of “key:value”.
If I want to refer to “name”, how do I say it? Python has two options for doing this:
1) Get the value using the .get()
2) Get value by key.
get() vs dict[key]
The difference between these options is that if you access a key that doesn’t exist, you’ll get an error KeyError.
person = { "name": "John", "age": 26 } print(person[“last_name”]) >> KeyError Traceback (most recent call last) >> KeyError: 'last_name'<br>
But if you use the method get(), you will get an empty value:
last_name = person.get("last_name") print(type("last_name")) >> NoneType
This method also has a second passed argument, which will be passed if there is no key:
last_name = person.get("last_name", "There is no last_name") print(last_name) >> 'There is no last_name'
JSON array
The array is enclosed in square brackets [ ].
["MALE", "FEMALE"]
Inside square brackets is a set of values separated by commas. There are no keys here, as in an object, so the array can only be accessed by element number. And therefore, in the case of an array, it is impossible to swap data inside. It’s an ordered set of values, so the order is important.
Values
Any value can be inside an array:
Numbers;
[1, 5, 10, 33]
strings;
["MALE", "FEMALE"]
Literals;
[true, false]
Arrays;
["MALE", "FEMALE", [1, 5, 10, 33]]
Objects;
[1, {a:1, b:2}, "John"]
Mixture.
[1, "John", true, [1, 5, 10, 33], {"a": 1, "b": 2}]
Well Formed JSON
JSON must be well-formed, i.e. syntactically correct.
JSON rules should be well-formed:
- Data is written as key:value pairs
- Data separated by commas
- Object is inside curly braces { }
- Array – inside square [ ]
To check JSON for syntax, any JSON Validator can be used. I’m using JSON Formatter, it not only checks for correct syntax but also formats the JSON into a readable visual format!
From this:
{"name": "John", "age": 26, "is_married": false, "children": { "name": "Julia", "age": null},"skills": ["Python", "Git", "Django", "Linux"]}
This will do this:
{ "name": "John", "age": 26, "is_married": false, "children": { "name": "Julia", "age": null }, "skills": [ "Python", "Git", "Django", "Linux" ] }
Conclusion
JSON (JavaScript Object Notation) is a text-based data exchange format based on JavaScript but can be used in any programming language. Easy to read by man and machine. Often used in REST APIs (more often than XML due to better readability and lighter weight).
Valid JSON values:
- JSON object – an unordered set of pairs “key:value”, enclosed in curly brackets { };
- an array is an ordered set of values separated by commas. It is inside square brackets [ ];
- number (integer or real);
- literals true (boolean “true”), false (boolean “false”), and null;
- line.
Useful Reads:
JavaScript Interview Questions