In Python stringValue
was given as a string variable:
stringValue = '{"DATA":{"VERSION":1.1, "STATE":True, "STATUS":"ONLINE"}}'
I can go ahead and "convert" or "cast" it as Python dictionary using eval
built in function:
result = eval(stringValue)
Is there a way to do the same in javascript if we would be given var stringValue
as:
var stringValue = '{"DATA":{"VERSION":1.1, "STATE":true, "STATUS":"ONLINE"}}'
CodePudding user response:
use eval
With one caveat ... you'll need to add (
and )
to the string
// this is the ORIGINAL value of the string in the question!!!
var stringValue = "{'DATA':{'VERSION':1.1, 'STATE':true, 'STATUS':'ONLINE'}}";
var result = eval(`(${stringValue})`);
console.log(result)
NOW that you changed the question
and make me look retarded - i.e. swapped '
and "
- now you CAN use JSON.parse
as a comment suggested
var stringValue = '{"DATA":{"VERSION":1.1, "STATE":true, "STATUS":"ONLINE"}}';
var result = JSON.parse(stringValue);
console.log(result);