edit - returned to indicate, first of all, that this is not a problem that can be solved with a regular expression.
It is important to distinguish between JSON notation as serialized form and JavaScript constant notation.
It:
{ x: "hello" }
is a perfectly valid JavaScript value (expression fragment), so this is:
var y = { x: "hello" };
gives exactly the same result as:
var y = { "x": "hello" };
In other words, the value of "y" in any of these cases will be exactly the same. In the exact same way, so it would be impossible to ever say which of these two constants was used to initialize the "y".
Now, if what you want to do is translate the line containing the JSON shorthand JavaScript style, without the quotes in the actual JSON, the only thing to do is parse it and restore the quotation line around the property names. That is, you will have to either write your own “relaxed” JSON parser than deal with unquoted identifiers as property names, or find a ready-made parser that can handle such a relaxed syntax.
In your case, it looks like once you have an accessible "relaxed" parser, everything is ready; you will not need to translate back. Fortunately, your "invalid" JSON response is completely interpreted by JavaScript itself, so if you trust the data source (and that big if), you should be able to evaluate it with "eval ()".
source share