How To Create JSON, What Is JSON

JSON is the most commonly used data format in web applications. It is simple and well accepted. In this post we will learn about it's advantages, creation and it's usage. But first lets understand what exactly is JSON.

What is JSON, How to create JSON





What Is JSON?


If we consider its full form, it is JavaScript Object Notation. It means it is a simple format for javascript objects. It has key value pairs enclosed in curly brackets { } and arrays enclosed in square brackets [ ]. The keys can have various value types like string, number, object, array, boolean values etc. JSON is a light weight structure to send data over network and is understood by all browsers. Shown below is an example of basic JSON

var json1 = { courseName : "JSON", topicsCovered : "5" };

var json2 = { 
                courseName : "JSON", 
                topicsCovered : "5", 
                topics : ["topic1", "topic2", "topic3", "topic4", "topic5"] };

Above two objects are JSON objects and very easy to use. Let's first see what are the advantages JSON has over other data formats.


Advantages Of JSON?


Before JSON XML was the most opted choice of data type. XML was long known and readable to human but JSON came and conquered over XML because:
  • It is clean and tidy.
  • It consumes less space over network.
  • It is much easier and fast to parse than XML.


How To Create JSON?


You can easily create JSON either by simply typing, the way I did above or you can simply create JavaScript objects like shown below


var json1 = {};
json1.courseName = "JSON";
json1.topicsCovered = "5";

var json2 = {};
json2.courseName = "JSON";
json2.topicsCovered = "5";

var topics = [];
topics.push("topic1");
topics.push("topic2");
topics.push("topic3");
topics.push("topic4");
topics.push("topic5");

json2.topics = topics;

Both the ways create same JavaScript objects. If you use Microsoft .Net, then you can use JSON Serializers to serialize your List<> or Array or any other data type to JSON.
JSON objects can be converted to string by using JSON.stringify() method. Let's see how?


How To Convert JSON Object To String?


JavaScript has JSON.stringify() method which converts an JSON object to string. Consider below example 


var json = { 
                courseName : "JSON", 
                topicsCovered : "5", 
                topics : ["topic1", "topic2", "topic3", "topic4", "topic5"] };

var JSONString = JSON.stringify(json);

console.log(JSONString);
//'{"courseName":"JSON","topicsCovered":"5","topics":["topic1","topic2","topic3","topic4","topic5"]}'
//This is a JSONstring having every key and value enclosed within double quotes.


How To Parse JSON?


There's a method as JSON.parse() which converts a JSON string to an object. Consider the below example 

var JSONObject = JSON.parse('{"courseName":"JSON","topicsCovered":"5","topics":["topic1","topic2","topic3","topic4","topic5"]}');

//Now JSONObject is proper object having all the fields from the JSON string.

JSON.parse() throws error if the JSON string is not correct like when curly brackets or quotes are missing.
This method is mainly used when data is retrieved from an ajax call.

So that's all about JSON. You can scroll our site for more such topics.

Do let us know if you have something to say.

Happy Learning!!!

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example