JavaScript

How to Convert String to Object in JavaScript

There can be situations when the programmer wants to convert a string to an object in order to manipulate it more easily. For instance, some JavaScript methods and functions only work with objects and not strings. In that scenario, you may need to convert the string to an object to use these methods or functions.

This blog will illustrate the methods for converting the string into an object using JavaScript.

How to Convert String to Object in JavaScript?

For converting a string to an object in JavaScript, use the following approaches:

Method 1: Convert String to Object Using “JSON.parse()” Method in JavaScript

Utilize the “JSON.parse()” method for converting the string to an object. It parses the string value into an object. For this, the mentioned method takes a JSON string as an argument, which needs to be parsed and then returns the JavaScript object created from the JSON string.

Syntax
Use the given syntax for JSON.parse() method for parsing a string to convert an object:

JSON.parse(JSONstring)

Here, “JSONstring” is the string that will be parsed into the JSON.

Example
Create a variable “myString” that stores a JSON string:

var myString = '{"name": "Linuxhint", "learn": "skills", "best": "website"}';

Now, verify the type of string “myString” using the “typeof” operator:

console.log("The variable 'myString' is: " + typeof(myString));

Call the JSON.parse() method and pass the string as a parameter and store the resultant object in variable “myObject”:

var myObject = JSON.parse(myString);

Print the converted object on the console utilizing the “console.log()” method:

console.log(myObject);

The output is evident that the string has been successfully converted into the object:

Method 2: Convert String to Object Using the “split()” Method

You can also use the “split()” method for converting string to an object. It divides/breaks a string in an ordered list of substrings to search for the specified pattern and gives the substrings in an array form.

Syntax
Follow the given syntax for the split() method:

split(pattern)

Here, the “pattern” corresponds to the character or expression on which the string will split.

Example
Create a comma-separated string:

var myString = "name,Linuxhint,learn,skills,best,website";

Call the split() method by passing the pattern or character (,) to split the string and store the substrings in variable “myArray”:

var myArray = myString.split(",");

Create an empty object named “myObject”:

let myObject={};

Iterate the array (splitted substrings) using the “for” loop and map the elements in the empty created object:

for (let i=0; i<myArray.length; i+=2) {
 myObject[myArray[i]] = myArray[(i+1)];
}

Finally, print the resultant object on the console:

console.log(myObject);

Output

That was all about converting string to object in JavaScript.

Conclusion

For converting a string to an object, use the “JSON.parse()” method or the “split()” method. In real-time projects, the “JSON.parse()” method is the most commonly and efficiently used method. It is important to note that it can throw an error if the string passed to it is not valid JSON. In this write-up, we demonstrated JavaScript’s methods for converting the string into an object.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.