Functions - JavaScript Convert to JSON
Posted by Superadmin on May 05 2023 05:45:09

JavaScript Convert to JSON

By Sivaraman VSivaraman V
  

JavaScript Convert to JSON

Introduction of JavaScript Convert to JSON

In javascript is the client side programming language for developing web pages and sent the client request to the servers with validation.JSON is the javascript object notation and also it is one of the response format from the server side responses. Using JSON.parse() is the pre-defined method for passing the values with open’({‘ and ‘)}’ brackets we can pass any number of values to these formats for converting the javascript variable values into the JSON format codes. We can use n number of methods for passing and transferring purposes into the script but mainly we used JSON.parse() method.

Syntax of JavaScript Convert to JSON

The javascript code always has some predefined methods for completing the user validation or any other client request processes. Each function has their own syntax and format will used in the web pages. We will see where we will use the default method in the javascript code.

<html>
<head>
<script>
var variablename='values';
var variablename1 = JSON.parse(variablename);
var variablename2 = JSON.stringify(variablename);
----some logic scripts—
We can sent the request to the servers using below codes
fetch(URL,{
method:"POST/GET"
body:JSON.stringify(datas);
headers:{
"Content-type":"application/json;charset=UTF-8"
}
</script>
</head>
<body>
</body>
</html>

The above codes are the basic syntax for the javascript method called json.parse() is used anywhere in the script for sent the request in UI and after that, the server code will receive the responses in the JSON format.

How does JavaScript Convert to JSON?

The javascript language is used for sent the request and receive the response from the server the received data will be in either xml or json format we also convert the received data into some other format like string conversion the method is stringify() method also its the function is used for changed the behavior of the string process method and the values are an array of string and calculated the number of objects that serves as a property of the object value is to be included in the JSON string. If suppose the server response is to be a null value or not provided in the strings or some other formats then properties of the objects are also included in the JSON format and received the server data it can be splitted into the spaces also it’s used for both strings and numbers instances in the white space is inserted and received output JSON as the string format easily used for readability purposes.

We can used some other online conversion from the entire javascript code into JSON format. After converting the script into JSON format the JSON text is must to be a readable format and also we need to convert that to an object and its implemented with the toString() method then it has been automatically converted into the java based instance it helps to write the results with another file with the readable format. Parallely we used JSON API for this scenario like Jackson JSON API etc. Basically JSON object is used for key-value pair technique for exchanging the data format including typically with rendered curly braces like both { and }. We also mention the datas in the script without a method like JSON.parsesimpily we declared the javascript variable types like var variable name ={ key:value,key1:value1};these line one of the examples for whatever we declared the specific variable with key-value pairs, the key must be unique so it’s not accepted duplicate keys but the value will be either duplicate or original the values are accepted sometimes null values also accepted in this case.

The JSON object will be working like text format but it must be converted into string format,arrayformat. We can used JSON like either dot notation or square bracket notations both are accepted. Mainly we used JSON.stringify() method for converting whatever the javascript values are declared in the script is converted into JSON values. We sent the request to the servers using fetch API it provides like an interface for fetching the resources across the network mainly it’s used for sent the request to the servers and received the response using JSON or Some other format. If suppose we need to receive the response using JSON format we will declared the fetch(“URI”).then(resp=>resp.json()) .json() is the method for receiving the response in JSON format after the client sent the request to the servers.

Examples of JavaScript Convert to JSON

Following are the examples of javascript convert to JSON:

Example #1

Code:

<!DOCTYPE html>
<html>
<head>
<title>
Welcome To My Domain
</title>
</head>
<body style="text-align:center;">
<h1 style="color:pink;">
Have a Gud day user
</h1>
<p id="first"
style=" font-weight: bold">
</p>
<button onclick="demo()">
Please click
</button>
<p id="second"
style="color:red;
font-weight: bold" ;>
</p>
<script>
var g = {
a_1: "siva",
a_2: "raman",
a_3: "sivaraman",
a_4: "arun",
a_5: "kumar",
a_6: "arunkumar"
};
var u =
document.getElementById("first");
var d =
document.getElementById("second");
//u.innerHTML =
//JSON.stringify(g);
function demo() {
//g.a_7 = "jegan";
d.innerHTML = JSON.stringify(g);
}
</script>
</body>
</html>

Output:

JavaScript Convert to JSON-1.1

After clicking on the button:

JavaScript Convert to JSON-1.2

Example #2

Code:

<!DOCTYPE html>
<html>
<head>
<title>
Welcome To My DOmain
</title>
</head>
<body style="text-align:center;">
<h1 style="color:pink;">
Have a Gud day user
</h1>
<p id="first"
style=" font-weight: bold">
</p>
<button onclick="demo()">
Please click
</button>
<p id="second"
style="color:red;
font-weight: bold" ;>
</p>
<script>
var g = {
a_1: "siva",
a_2: "raman",
a_3: "sivaraman",
a_4: "arun",
a_5: "kumar",
a_6: "arunkumar"
};
var u =
document.getElementById("first");
var d =
document.getElementById("second");
u.innerHTML =
JSON.stringify(g);
function demo() {
g.a_7 = "jegan";
d.innerHTML = JSON.stringify(g);
}
</script>
</body>
</html>

Output:

Output-1.3

Output-1.4

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<h2>Welcome To My Domain</h2>
<p id="demo"></p>
<script>
var t = '{"students":[' +
'{"firstName":"Siva","lastName":"Raman" },' +
'{"firstName":"Arun","lastName":"Kumar" },' +
'{"firstName":"ST","lastName":"Jubb" }]}';
result = JSON.parse(t);
document.getElementById("demo").innerHTML =
result.students[0].firstName + " " + result.students[0].lastName;
result.students[1].firstName + " " + result.students[1].lastName;
result.students[2].firstName + " " + result.students[2].lastName;
</script>
</body>
</html>

Output:

Output-1.5

The above examples show the conversion of javascript to json the first two examples we used JSON.stringify() method and the final example JSON.parse() is used in the script.

Conclusion

Finally, we concluded the javascript codes into JSON format through an online/offline process and also basically it will display the text format but we use some default method like JSON.stringify() method for convert JSON object to string and again reconverted the data into the javascript object using JSON.parse() method.