Convert String to object JavaScript | Example code

  • by

Best solution to convert String to object is using JSON parse() method in JavaScript. Create a variable and use JSON.parse(“string”) to have it converted into an object.

JSON.parse(text[, reviver]);

Example String to object JavaScript

Simple example code.

<!DOCTYPE html>


   var string = '{"firstName":"name1", "age":30, "City":"NY"}';
   var myobj = JSON.parse(string);





Convert String to object JavaScript

If JSON string without the curly braces.

var string = "{firstName:'name1', lastName:'last1'}";
eval('var obj='+string);


Output: Object { firstName: “name1”, lastName: “last1” }

Do comment if you have any doubts or suggestions on this JS Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.