The Basics of JSON

Foridul Islam
3 min readAug 10, 2022

--

A complete guide for beginners to work with JSON data

JavaScript Object Notion, commonly known as JSON, is one of the most popular data transition formats. It is a text-based and lightweight format for data transactions. It is easy for humans to read and write. It is easy for machines to parse and generate.

The Uses of JSON

JSON is a lightweight format that is easy to use for data interchanging.

An example of where this is used is web services responses. In the old days, web services used XML as their primary data format for transmitting back data. But since JSON appeared, it has been the preferred format because it is much more lightweight.

Build of JSON

JSON is built on two structures:

(i) A collection of key-value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

(ii) An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Properties of JSON

  • JSON filename extension is .json
  • JSON content type is application/json

Syntax and Structure of JSON

The syntax of JSON is as follows:

  • Data is in Key-Value pairs
  • Data is separated by commas
  • Objects are enclosed in curly brackets ({})
  • Arrays are enclosed in square brackets([])

The structure of a JSON looks like

{ "key": "value" }

Example of a JSON

{
"_id" : "1",
"Name" : "John",
"Age" : 23,
"Income" : 1000.0
}

Data types of JSON

In JSON, values must in one of the following data types.

  • Number:Double precision floating-point
  • String:Series of characters
  • Boolean: true or false
  • Array:An ordered sequence of values
  • ObjectAn unordered collection of key/value pairs
  • ValueString, Number, Boolean, null, etc.
  • null:Null or Empty

Static Methods of JSON

The JSON has mainly 2 static methods in JavsScript.

(i) JSON.parse()

(ii) JSON.stringify()

(i) JSON.parse()

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

Suppose, a JSON string is as follows,

'{"_id":"1","Name":"John","Age":23,"Income":1000}'

To convert the JSON string to a JSON object,

const personDataString = '{"_id":"1","Name":"John","Age":23,"Income":1000}';let personObj = JSON.parse(personDataString);

The personObj we will get is

{
"_id" : "1",
"Name" : "John",
"Age" : 23,
"Income" : 1000.0
}

Now we can access the name of the person like

console.log(personObj.Name); 
//John

(ii) JSON.stringify()

The JSON.stringify() the method converts a JavaScript object or value to a JSON string.

Suppose, a JSON is as follows,

{
"_id" : "2",
"Name" : "Don",
"Age" : 27.0,
"Income" : 3000.0
}

To convert the JSON object to a JSON string,

const personObj = {
"_id" : "2",
"Name" : "Don",
"Age" : 27.0,
"Income" : 3000.0
}
let personDataString= JSON.stringify(personObj);

Now the person data string looks like this,

console.log(personDataString); //'{"_id":"2","Name":"Don","Age":27,"Income":3000}'

Methods of JSON

The following are available for the JSON class methods.

  • Add:Add an item to a JSON array
  • Array:Create an Array JSON object
  • Object:Create an Object JSON object
  • Boolean: Create a Boolean JSON object
  • BooleanValue:Get the boolean value of the JSON object
  • CountNumber of items in the object
  • copy:Copy the JSON object
  • DeepCopyCopy the JSON object
  • Parse:Create a JSON object from serialized JSON
  • String:Create a String JSON object
  • Stringify:Serialize a JSON object
  • StringValue:Get string value of JSON object
  • ToString:Serialize a JSON object
  • True:Create a true Boolean JSON object
  • False:Create a false Boolean JSON object
  • Type:Get the type of JSON object
  • Delete:Delete a JSON object/array item value
  • Insert:Insert an item into a JSON array
  • Item:Return or set JSON object/array item value
  • NumberByName:Get the number of named items in the JSON object
  • NumberValue:Get the number value of the JSON object

Summary

To use JSON, it must be converted into a structure. Serialize and deserialize are terms that are commonly used when dealing with JSON.

Thanks for reading!

--

--

Foridul Islam
Foridul Islam

Written by Foridul Islam

Sr. Software Engineer at SELISE Digital Platforms

No responses yet