What is JSON?

Reading Time: 3 minutes

What is JSON?

In this article I will introduce JSON for beginners, so let’s see What is JSON? and how it works with some examples.

JSON ( JavaScript Object Notation) is a text format for structuring and exchanging data.

Primarily, it is used to transmit data between a server and client application (Mobile, web, or desktop) as an alternative to XML. almost all Web APIs uses JSON nowadays to transmit data.

Learn more about Web APIs in this article.

In other words, we can Describe JSON as a syntax for storing and transmitting data and it’s text-based.

 

JSON is built on two structures:

  • A collection of name/value pairs. In various programming languages, this is called as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is called an array, list, or sequence.

 

json h-educate

Keys and Values

JSON objects are built mainly on two things (keys and values). Together they make a key/value pair.

  • Key: A key is always a string enclosed in quotation marks.
  • Value: A value can be a string, number, boolean expression, array, or object.

Example of Basic JSON Object:

"userName" : "hassan"

Here is another example of a JSON data object:

 

{
     "firstName": "hassan",
     "lastName": "aboul hassan",
     "address": {
         "streetAddress": "my address",
         "city": "New York",
     },
     "phoneNumbers": [
         "0096170821471",
         "0096100000000"
     ]
 }

JSON versus XML

As noted in the introduction, the main alternative to JSON is XML. However, XML is becoming less and less common in new systems, and it’s easy to see why.

The following is the same data shown above but now in XML:


<person>
      <firstName>hassan</firstName>
      <lastName>aboul hassan</lastName>

      <address>
         <streetAddress>my address</streetAddress>
         <city>my address</streetAddress>
      </address>

      <phoneNumbers>
         <phonenumer>XXX-XXXX-XXX</phonenumber>
         <phonenumer>XXX-YYY-ZZZ</phonenumber>
      </phoneNumbers>
</person>

You can notice that the file is larger and contains more data!

When should I use JSON?

Whenever you have an API or sending data between your server and your client applications, JSON will be the best choice, it’s lighter and with the help with many libraries nowadays, it’s very easy to handle and work within most programming languages.

 

Other examples:

Sending DATA:

var myObj = { "name":"John", "age":31, "city":"New York" };
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;

Receiving DATA:

var myJSON = '{ "name":"John", "age":31, "city":"New York" }';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;

 

I hope this gave you an overview of JSON and how it works.

Please if you have any question, feel free to leave it in the questions area.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *