S
S
saalaus2018-09-29 08:58:07
JavaScript
saalaus, 2018-09-29 08:58:07

How can I parse such a string?

I have a similar string
"{one:{hello:[{two:three}]}}"
How can I turn it into a JSON object if the function JSON.parse()throws an error

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AngReload, 2018-09-29
@saalaus

You need to use the JSON5 decoder
https://json5.org/
Oops , didn't notice you didn't have three in quotes. Then it won't fly.
Then you can try like this:

var myjson = unquotedJson.replace(/(['"])?([a-zA-Z0-9]+)(['"])?/g, '"$2"');
JSON.parse(myjson)

but this is unreliable, if the format of the input string is not known, maybe there will be Russian letters or something else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question