Skip to main content

Introduction to JSON – Part 1

json-img
JSON is a data interchange format, frequently used for data exchange between clients and servers across the web. I am dividing this short tutorial series into three parts just to make it easy for you to understand. Part 1 is an introduction to JSON. It shows what is JSON, its structures are and how to write a JSON document. Part 2 explains How to process JSON in JAVA. The last part includes JSON Serialization. Hold your breath, a lot is coming up.
JSON, short for JavaScript Object Notation is a lightweight format which makes data exchange more than easier. It is also used to store data. JSON is language independent. For example, you can make a JSON Object in java and send it to a C# program which will gracefully handle it. JavaScript object can be converted into a JSON object and vice versa.
A JSON document can have two structures.

  • JSON Object: A name value pair or a key value pair. Names/Keys are only strings however values are of various types. Name and value are separated by a single colon (:). JSON Object is contained within the curly braces.


Name : Value,
}

  • JSON Array: An ordered sequence of values stored in square braces separated by a comma.

[
Value,
]
Types of Values: These types should be familiar to you and may not need any explanation. A value can be a

  1. String
  2. Number
  3. Bool (True/False)
  4. JSON Object
  5. JSON Array

A JSON value can also be null.
The file which contains JSON data is stored with .json extension. Following is an example document named Example.json. Study it and identify different structures and value types.
Example: Example.json
{
"first_name": "Salman",
"last_name": "Ahmad",
"gender": "male",
"age": 22,
"hobbies": [
"video games",
"drawing",
"music"
],
"fav_books": [
{
"name": "The Shallows",
"author": "Nicholas G. Carr"
},
{
"name": "Brain Bugs",
"author": "Dean Buonomano"
}
]
}
Next is How to process JSON data in JAVA. We will use this example in our code. See you there.

Comments