CodeNewbie Community šŸŒ±

Cover image for What is JSON? And why do you need it?
Rizwan Hasan for TechLearners

Posted on • Edited on • Originally published at dev.to

What is JSON? And why do you need it?

Before going on to the topic JSON, I would like to discuss a simple example because it will be a lot easier to explainĀ JSONĀ after the example.

Thumbnail

Suppose we are developing desktop software or a web application. So, for the desktop software we need to save the changes of settings that users have made so that when he/she opens the software again at another time, he/she doesnā€™t need to change the settings again as he/she changed it earlier. Also, itā€™s a very annoying thing for a user to config the same changes at every start. Now, what about web applications? Web apps have one or more databases to store changes. Letā€™s think about a drawback here. Web apps need to store many temporary configuration or data to serve the user with a good experience for that session. And itā€™s an obvious thing that, developers canā€™t let an app to make the database busy for some temporary session data. Now, the question comes about how we store these changes or temporary data on storage? Confused? Again thinking about usingĀ databasesĀ likeĀ Oracle,Ā Mariadb? Of course, we can do that but itā€™s not an efficient solution here. Ok, Keep down your thinking a bit and Iā€™m keeping all the hard things aside, just store the data as aĀ Text file. Wow, our storing problem is solved. But a new problem is knocking at the door, how to retrieve the data and get the sameĀ data structuresĀ in the same manner that we had applied in the application? On a simple text file, there are lackings of data formation for theĀ data structuresĀ that we use in any program. Here comes the life-saving concept of data serialization, also we are very close to jumping onĀ JSON.

What is Data Serialization?

Data Serialization is the process of converting structured data to a format that allows sharing or storage of the data in a form that allows recovery of its original structure when needed. The reason for serializing data is finding some sort of universal format that can be easily shared across different applications.

What is JSON?

JSONĀ is a data interchanging format that uses human-readable text to transmit data objects consisting of data structure and itā€™s the most widespread format for data serialization. SimplyĀ JSONĀ is text, written with JavaScript object notation.Ā ā€œJavaScript Object Notationā€ is the full form ofĀ JSON. Many developers donā€™t likeĀ JavaScriptĀ because of their perspectives. But donā€™t worry, itā€™s alright if your knowledge is empty aboutĀ JavaScript.

JSON Example

Why JSON?

JSONĀ is mainly intended for data interchanging among the applications. Parsing data from one application to another throughĀ JSONĀ is so much easy because of itsĀ language-independentĀ data format. Almost every programming language has JSON support through official and 3rd party. Now, referencing my example,Ā JSONĀ can be used as a configuration or temporary data storing file also for any application. There is an important fact thatĀ JSONĀ lacksĀ Abstract Data TypeĀ (ADT) feature because of its data serialization format which breaks the opacity ofĀ ADTĀ by potentially exposing private implementation details.

A very popular database named ā€œMongoDBā€ uses JSON-like documents with the schema.


Basics of JSON

JSONĀ uses ā€œ.jsonā€ for filename extension. You can write JSON file using normal text editors likeĀ Windowsā€™s built-in Notepad.Ā JSONĀ supports String, Number, Object (JSON object)/Hashmap, Array/List, Boolean and N*ull* data types.

Letā€™s see some examples

String

{
  "name": "Rizwan Hasan"
}
Enter fullscreen mode Exit fullscreen mode

Number

{
  "age": 20
}
Enter fullscreen mode Exit fullscreen mode
  • JSON names require double quotes ("").
  • JSON uses a colon (ā€œ:ā€) for separating names and values.

Objects / Hashmap

{
  "student": {
    "name": "Rizwan Hasan",
    "age": 21,
    "sex": "Male"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Here key ā€œstudentā€ has an object value and that object has some names.

  • JSON uses curly braces (ā€œ{}ā€) for object separation and commas (ā€œ,ā€) for names separation.

Array / List

{
  "students": [
    "Rizwan",
    "Sakib",
    "Natsu"
  ]
}
Enter fullscreen mode Exit fullscreen mode
  • Here key ā€œstudentā€ have an array value and that array has some name element.
  • JSON uses square brackets (ā€œ[]ā€) for array declaring and commas (ā€œ,ā€) for element separation.

Boolean

{
  "answer": true
}
Enter fullscreen mode Exit fullscreen mode

Null

{
  "nickname": null
}
Enter fullscreen mode Exit fullscreen mode

Data types in Nested manner

{
  "students": [
    {
      "id": 101,
      "name": "Rizwan Hasan",
      "age": 21,
      "department": "CSE",
      "sex": "Male",
      "paid": true,
      "cgpa": 2.13
    },
    {
      "id": 102,
      "name": "Faria Hasan",
      "age": 20,
      "department": "BBA",
      "sex": "Female",
      "paid": true,
      "cgpa": 3.56
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

Here we are storing some student's information. First, there is an object whichā€™s key is named ā€œstudentsā€ and the value is an array blocked with square brackets.Ā In the array, there is two studentā€™s information. Every element of the array is also an object and they contain some information name, age, department, sex, paid, and GPA. Name, department, and sex are strings. Id, age, and, GPA is numbers. Lastly paid is boolean.


Conclusion

So far I discussed JSON but still, there are a lot of things to know about like how to use it with different programming languages and how to do data interchanging between two or more languages. Here I only focused on giving the basics because after this pretty basic you are ready to go with JSON more advanced and also with the upgraded version of JSON calledĀ YAML. To learn more about JSON, I suggest this free online tutorial from TutorialspointĀ here. In the future, I will try to cover how to use JSON with popular programming languages like Python, Java, Kotlin, and C++.


Share your opinion in the discussion section below and of course the questions if any. Don't forget to follow us.

šŸ’” AND SUBSCRIBING to our YouTube TechLearnersInc and Telegram t.me/TechLearners will be amazing.


šŸ“Œ Featured in DEV Community's Twitter

Top comments (0)