How can I convert a string to object in Javascript?

Joseph Rback

I know this question has been answered many times before. But my string is a little different from others. Here's my data:

let str = "{'name':'joe', 'text':\"that's amazing\", 'registered':True, 'height':193, 'related':{'foo':'bar', 'some':'thing'}}"

and I am looking for something like: :

let obj = { name: 'joe',text:"that's amazing", registered:true, height: 193, related:{foo:'bar', some:'thing'}}

Mamun

You can try using replace() and JSON.parse():

let str = "{'name':'joe', 'registered':true, 'height':193, 'related':{'foo':'bar', 'some':'thing'}}";
let obj = str.replace(/'/g,'"');
obj = JSON.parse(obj);
console.log(obj);

Update: Since you are having apostrophe in your string, you can try using JSON5 API which is compatible with the JSON API:

let str = "{'name':'joe', 'text':\"that's amazing\", 'registered':'True', 'height':193, 'related':{'foo':'bar', 'some':'thing'}}";
let obj = JSON5.parse(str);
console.log(obj);
<script src="https://unpkg.com/json5@^2.0.0/dist/index.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert this string to a javascript Date object?

how can i convert object into an array in javascript?

How I can convert array to object in Javascript

How can i convert a string object to string pool object?

How can I convert a string to boolean in JavaScript?

How can I convert string to array in JavaScript?

How can i convert an object to the structure { [param: string]: string | string[] }?

How can I convert a single object of a json string into a .Net object?

How can I convert a nested javascript object into an inline string of CSS styles?

How can I convert a Request object into a stringifiable object in JavaScript?

How can I convert a string to char[] without copying the object?

How can I convert an object to a string (Twig & Symfony)?

How can I convert object keys & values to formatted string?

How can i convert an array in Json string to c# object?

How can I convert ArrayList<Object> to ArrayList<String>?

Scrapy - how to convert string into an object which I can use XPath on?

How can I convert a string of bytes to a byte object

How can I convert IList<object> to string array?

How can I convert JSON-like string to PowerShell object?

How do I convert a string to object and use inside loop javascript?

How may I convert a JavaScript [object String] to HTMLElement?

How to convert the string to object in JavaScript?

How to convert a String into an Object in JavaScript

How to convert object into string in javascript?

How to convert object to string javascript

How can I convert the "arguments" object to an array in JavaScript?

How can I convert the object array to GraphQL format in Javascript?

How can I convert a JavaScript object(JSON) into JSV format?

How can I convert .geojson file to a JavaScript object with nested arrays?