The Basics of JSON
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-pointString:
Series of charactersBoolean: true
orfalse
Array:
An ordered sequence of valuesObject
An unordered collection of key/value pairsValue
String, 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 arrayArray:
Create an Array JSON objectObject:
Create an Object JSON objectBoolean:
Create a Boolean JSON objectBooleanValue:
Get the boolean value of the JSON objectCount
Number of items in the objectcopy:
Copy the JSON objectDeepCopy
Copy the JSON objectParse:
Create a JSON object from serialized JSONString:
Create a String JSON objectStringify:
Serialize a JSON objectStringValue:
Get string value of JSON objectToString:
Serialize a JSON objectTrue:
Create a true Boolean JSON objectFalse:
Create a false Boolean JSON objectType:
Get the type of JSON objectDelete:
Delete a JSON object/array item valueInsert:
Insert an item into a JSON arrayItem:
Return or set JSON object/array item valueNumberByName:
Get the number of named items in the JSON objectNumberValue:
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!