Last Updated on February 13, 2023 by Prepbytes
JSON has been trending ever in the field of web development with JSON being declared as the better successor for SOAP-based applications that are ultimately based on XML for its data transfer. JSON is based on a group of data that we can name as JSON stack as discussed in the forthcoming sections of this article on JSON stack.
What is JSON?
JSON stands for Javascript Object Notation, it is one of the easier ways to transmit data as compared to the old and inefficient method of data transfer through XML. The data transfer is performed over devices connected through a network. JSON can be used for any AJAX-based (Asynchronous Javascript and XML) application that runs on XML data.
In AJAX, the code is passed from the server at the backend to the coded Javascript. Although, it has become a standard notation to be used and is used for other languages as well such as Python etc. The syntactical structure of JSON consists of name-value pairs that can be compared to what comes as built-in in programming languages in the form of the object, records, dictionaries, hash tables etc when unordered and array, vector lists etc. when the values are enumerative or ordered.
Example of JSON
Now that we have some firm knowledge of the topic, let us head over to see an example of a JSON file as mentioned below. From this, we can see that JSON permits nesting and the names in name-value pairs are defined as string type.
{
"name": "John Doe",
"age": 32,
"address": {
"street": "123 Main St.",
"city": "Anytown",
"state": "CA"
},
"phoneNumbers": [
{
"type": "home",
"number": "555-555-1234"
},
{
"type": "work",
"number": "555-555-5678"
}
]
}
JSON vs Javascript Object
While Javascript Object Notation and Javascript Object may be assumed to be a mirror image of each other but they are entirely different as some of the JSON are not Javascript Objects and some of the Javascript Objects are not JSON.
We can conclude this statement by taking an example mentioned below:-
var one = {a:b} is a Javascript Object and not a JSON, the reason being that a and b are not string type, one tweak that can be performed to turn them into JSON can be, var one = {“a”:”b”}, where one is an object of type string.
Components of JSON Stack
JSON Stack can be classified on the basis of its components to classify the different types of data that it can hold for the transmission of data across networks in a lightweight manner. Remember that data must be in name-value pairs and separated with the help of a comma. Curly brackets contain the objects and square brackets contain the array.
-
Object
It can be termed an unordered set consisting of name-value pairs. It starts with an opening brace ( { ) and commences with a closing brace ( } ). The name is separated by pair with the help of a colon and the pairs are separated using a comma (, ). -
Array
It can be defined as a collection of values that are ordered or sequential. It starts with an opening bracket ( [ ) and ends with a closing bracket ( ] ) while the values are separated with the help of the comma operator ( , ). -
Value
These can be nested and are any one of the following, namely as, object, array, number, string in double quoted form, boolean values such as true or false or even set to NULL.
- String
It can be demonstrated as a group of Unicode characters enclosed by double quotes for the opening and closing of the string. It is a similar paradigm to strings that we have in programming languages in Java or Python. Even a single character string will be identified as a string.
Conclusion
In this article, we studied the basics of JSON Stack, starting from what the JSON Stack and JSON Definition stand for, we decrypted how to decipher the confusion between Javascript objects and JSON. In the later stages of the article, we studied the JSON stack which is composed of different components and studied an example of JSON as well.
We hope you have a concrete command of the topic and expect to see you again at PrepBytes with another informative article from our side.
Frequently Asked Questions
1. How to convert the Javascript object into JSON?
Javascript Object can be converted into JSON using the parse() function, where the name must be a string mandatorily
2. List the use cases for JSON.
JSON is used with APIs and Web Services as it is fast and lightweight for data. Also, it can be used with multiple programming languages apart from Javascript such as Java, Python and Ruby etc.
3. Can we put comments in JSON?
Comments are one of the limitations of JSON as we cannot put any comments in JSON.