How to Convert String to Object in JavaScript

To convert a String to an Object in JavaScript, use the JSON.parse() method. The JSON.parse() is a built-in JavaScript method that parses a string and returns an object.

When working with an API, we receive the data as string objects. So, we need to convert that string data into proper objects. Also, we need to pass this data as strings into the API, so we need a method to convert a string to an object.

To convert JavaScript Object to String, use the JSON.stringify() method. There are several ways to convert String to Object, but we will see the most efficient way.

JavaScript JSON.parse()

The JSON.parse() method parses a string and returns an object. The JSON.parse() function takes one required and one optional parameter to perform some operation and returns an object.

Syntax

JSON.parse(string,function);

Arguments

  1. string:- It is a string that can be valid for any data type.
  2. function: It is an optional argument. If we want to perform some operation on our string, we can pass this to our custom function.

Return value

The return value is based on what we pass in his parameter as a string. It generally returns an Object, but still, it depends on what you give as a string.

Example

const string = '{"name":"paresh","age":34,"professional":"coder"}';

const boolean = '{"isAdmin":true}';

const array = '["mango","banana","apple"]';

console.log(JSON.parse(string));
console.log(JSON.parse(boolean));
console.log(JSON.parse(array));

Output

{ name: 'paresh', age: 34, professional: 'coder' }
{ isAdmin: true }
[ 'mango', 'banana', 'apple' ]

In the above example, we pass a JSON string to the JSON.parse() method, and in return, we get a JavaScript object. We also parse our array and a boolean value to its original form.

If you pass an array as a string to the JSON.parse() method, it returns an array. So let’s see how we give a function inside a JSON.parse() method to perform some operation on it.

const string = '{"name":"paresh","age":34,"professional":"coder"}';

console.log(JSON.parse(string, (key, value) => {
     return key === "age" ? `${value} year` : value;
}));

Output

{ name: 'paresh', age: '34 year', professional: 'coder' }

In the above example, we convert “year” in the age property by parsing a custom function inside JSON.parse() method.

Manual convert String to object in JavaScript

const string = "name,paresh,age,34,professional,coder"

const temp = string.split(",");
const obj = {}
let i = 0;
while (i < temp.length) {
  obj[temp[i]] = temp[i + 1];
  i += 2;
}

console.log(obj)

Output

{ name: 'paresh', age: '34', professional: 'coder' }

In the above example, we have one string. So, firstly we split that string into an array using the string split() function. And then, we iterate that array and store that value in our object.

Conclusion

If you are working on a real-time project, then the manual way is not efficient; instead, use the JSON.parse() method to convert your string to an object.

That’s it for this tutorial.

Related posts

How to Convert PHP Array to Javascript Object

How to Convert String to Char Code

How to Convert URL to String

How to Convert String to Date

How to Convert Object to Array of Objects

Leave a Comment