What is JSON
But it is not a programming language it is a data format or you can say data interchange format and it is the most used format in the industry right now. It is easy for humans and for machines to read and generate. You may be thinking that why we use JSON the very simple answer is “To Communicate”. we humans use some language may be Hindi or English or any other language to communicate with other humans and the same way computer programs use JSON to communicate with other computer programs.
We use JSON to build RESTful APIs and ConfigFiles, for example, the settings of VS Code or if you use NodeJS then there you also have a package.JSON these are the examples, of config files and you might have already seen the restful API that also uses JSON. JSON become very popular because it was extremely lightweight and easy to understand. Earlier people were using XML but now everyone uses JSON. JSON is language independent and almost all the programming languages have the inbuilt functionality to work with JSON.
Now let’s discuss the data type of JSON. We have a string and anything written inside double quotes is a string to represent names long text values, a whole document’s content can be represented as a string then we have numbers so numbers are any number a whole-number decimal number negative number and you can write it directly in your JSON. Boolean values can have either true or false and null is defined as null and which means no value is available.
Then we have the most important thing which is an Object is defined with curly braces and the inside the object we represent data in key-value pairs one key-value pair is one item of the object and an object can have any number of items separated by a comma. If you have only one item then a comma isn’t needed but multiple items are separated by a comma.
In the JSON object key can only be defined with a string so the key is always written in double-quotes and for the values, we can use any other data type that we see here and we can even use arrays as the value of a key and that is how we represent the complex data by nesting the JSON documents with arrays and objects.
Finally, we have an array and it is defined by square braces inside square braces you can put any number of items separated by commas you can also put different data types for your array elements so these were the basic things about JSON now let’s see some examples. So we have a file named user.JSON and inside this file, you can write any JSON data and it is a valid JSON, for example, you can write a string, you can write a number value, you can write boolean and you can write an object.
But writing a single value does not make any sense because we need to represent some complex data with the help of JSON so usually what happens is in the root of our JSON file we have a JSON object and we represent a JSON object with the help of curly braces and let’s represent a user with the help of this JSON object. So inside this JSON object, the first thing we can have for the user is the user id, and let’s make the id as 1 then we can have the username and username can again be any string, for example, belal then we can have the email of the user so this is how we create a JSON object now,
so you can write any number of items for your array, then we can also define one more complex object and this time we will define all the post by this user and for this posts again we can use an array but inside this array, we will have the objects this time so this is an array of objects and for the array items we can also define an object like this,
And this object will contain the post again give it 1 then the title of the post let’s say learn JSON and the content of the post so whatever content that you want you can put it here and the same way we can define multiple posts for this user so this is how we define a JSON file or JSON object and it is used in all the cases for developing android apps for building websites the JSON.