This article will discuss and demonstrate how to create a JSON object and convert it into a string to send it back to a server.
Create a JSON Object in JavaScript
First, we must create an object of the Object class using the new keyword. Then, we can store its reference inside the variable, as shown below.
To add properties inside the object, we can use the dot notation. In myObj, we add four properties: name, email, mobile (an array), and age.
myObj.name = "Adam"; myObj.email = "adam@abc.com"; myObj.mobile = ['123456', '987654']; myObj.age = 30;
The good thing about the object is that we can assign any data type to a property of the object. In the example above, we see that we have assigned a string, array, and number as the values to the object’s properties.
If we print the data type of the myObj using the type keyword, we will get the type as an object.
Output:
If we want to send this object to the server, it must be in string format. That is why we first need to convert this object into a string before sending it.
Convert a JSON Object Into a String Using JSON.stringify() in JavaScript
The JSON.stringify() method is used to parse the JavaScript object into a JSON string.
Syntax:
JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space)
The JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 0 (mandatory) can be an array or an object. The JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 1 (optional) alters the JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 2 process.
The JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 3 (optional) parameter takes an integer value and inserts space (including indentation, line break characters, etc.). This parameter can be used for readability purposes.
If the JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 3 parameter is not specified, no space will be added in the output JSON string. The default value for the JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 1 function and the JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 3 parameter is JSON.stringify(value) JSON.stringify(value, replacer) JSON.stringify(value, replacer, space) 7.
To convert the JavaScript object myObj which we have created, we will directly pass it as a parameter to the JSON.stringify() method as shown below.
myObj = JSON.stringify(myObj);
After converting the JavaScript object, we will store it inside myObj. Finally, if we check the data type of myObj, we’ll see myObj = JSON.stringify(myObj); 2 instead of myObj = JSON.stringify(myObj); 3, as shown below.
In this article we are going to discuss how to convert a JSON string into a JavaScript object with suitables examples in JavaScript.
There are two possible ways to convert a JSON string into a Javascript object – eval() and parse(). The usage of eval() method is unsafe and not preferred. It is vulnerabable to hackers. The parse() method is preferred in general anytime.
The typical application for JSON is Data transfer to and from a web server. The data that has been sent from a server is always a JSON string. Using JSON.parse(), we can convert the JSON string into a JS object.
Syntax
The syntax on how to convert a JSON string into JS object is −
Where, text is the string whose value is to be converted into an object.
Example 1
In this example, we use parse() method to convert a JSON string to object. The dictionary is taken as a JSON string.
HTML as a JSON
To convert a JSON string into a JS objectOn executing the above code, the following output is generated.
Example 2
In this example, we use parse() method to convert a JSON string to object. The dictionary is taken as a JSON string.
Array as a JSON
To convert a JSON string into a JS objectOn executing the above code, the following output is generated.
Example 3
The below example program illustrates about converting a JSON string into JS object where the date is given as a string in JSON string. Because JSON string does not accept Date objects.