-4

How can I convert the string to an Object?

var string = "{ test: 'hello' }";
insberr
  • 39
  • 9
  • Does this answer your question? [Safely turning a JSON string into an object](https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – rmlan Oct 20 '20 at 22:13

1 Answers1

0

This is very straightforward, please search google before posting.

const obj = JSON.parse(string);
Victor Oliveira
  • 352
  • 2
  • 10
  • 1
    Actually, that won't work as the O.P.'s example is not JSON. You could parse it as JSON5 though: https://json5.org/ — just need to `const JSON5 = npm require('json5')` and then `const obj = JSON5.parse("{ test: 'hello' }")`. – Nicholas Carey Oct 20 '20 at 23:54
  • Yeah json5 will work thanks @NicholasCarey I forgot that json5 existed – insberr Oct 21 '20 at 05:11
  • OP marked JSON as one of the tags, but I guess you're right for the lack of quotes between the key. Still, not a very well formed question – Victor Oliveira Oct 21 '20 at 22:26