Working with JSON

Anjali Nirmani
3 min readApr 28, 2019

--

What do you know about JSON. It is interesting to know and it gives us a human-readable collection of data that we can access in a really logical manner.

JSON

JSON stands for JavaScript Object Notation. As mentioned in the name, it has been extended from JavaScript scripting language. But keep in mind, JSON is available for many other languages like Python, PHP, Java and Ruby. It is simply a format, that you can transfer data from client to server and from server to client.

If we use JSON as a stand-alone file, the extension would be .json and if we use JSON in another file format (ex: html), it appears inside of quotes as a JSON string or JSON can be an object assigned to a variable.

Some general uses of JSON:

  • It is used while writing JavaScript based applications that includes browser extensions and web sites.
  • JSON format is used for serializing and transmitting structured data over the network connection.
  • It is primarily used to transmit data between a server and web application.
  • Web services and APIs use JSON format to provide public data.
  • It can be used with modern programming languages.

Characteristics of JSON

  • JSON is easy to read and write
  • It is a lightweight text-based interchange format
  • JSON is language independent

JSON Format

JSON object is a key-value data format that is rendered in curly braces. Each key-value pair is separated by a comma and key is separated from the value using a colon as in “key” : “value”. Within each object, “key” should be a unique string. Although you can use the whitespaces in between the key string, it’s best to use underscores because it much more readable.

“value” can be any valid data type like string, numbers, objects, arrays, boolean values or maybe even a null.

Let’s see a simple JSON object that is saved in .json file.

Not only that, we can use JSON objects in .html file or .js file.

Additionally, we can use JSON as a string rather than an object. This can be useful when transporting data in a quick manner.

Nested Objects

We can create multiple objects as a nested object in JSON. Its something similar to the following example:

In here also, we use commas to separate the elements and objects.

Nested Arrays

Data can also be nested within the JSON format by using JavaScript arrays. We may use arrays to dealing with a lot of data that can easily group with together.

It looks like this:

JSON Functions

  • json_encode : Returns the JSON representation of a value
  • json_decode : Decodes a JSON string
  • json_last_error : Returns the last error occurred

# JSON.stringify() function converts an object to a JSON string.

# JSON.oarse() function converts a string to a JSON object. This is a secure function to parse JSON strings and convert them to objects.

Advantages of JSON

  • JSON is faster - JSON syntax is very easy to use. We have to use only -> as a syntax which provides us with an easy parsing of the data and faster execution of the data
  • Schema Support - It has a wide range of supported browser compatibility with the operating systems. So the applications made with the coding of JSON doesn’t require much effort to make it all browser compatible
  • Server Parsing - On the server side parsing is the important part that developers want. In this case, JSON server-side parsing is the strong point that indicates us to use the JSON on the server side
  • Tool for sharing data - JSON is the best tool for sharing data of any size. This is because JSON stores the data in the arrays, so data transfer makes easier

Feel free to share your knowledge or add comments if you have any feedback regarding this post :) Hope to see you again with another interesting topic. Thank you.

--

--