Skip to content

Instantly share code, notes, and snippets.

@larruda
Last active November 22, 2022 09:49
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save larruda/967110d74d98c1cd4ee1 to your computer and use it in GitHub Desktop.
Save larruda/967110d74d98c1cd4ee1 to your computer and use it in GitHub Desktop.
REGEX to add quotes to JSON unquoted keys (turns an invalid JSON into a valid one).
json_string.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":');
eval('var json = new Object(' + json_string + ')');
@rajesh-vi
Copy link

how to deal with object that contains dot in key?

@davidlnl
Copy link

/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9_]+)(['"])?:/
for keys contain underscore
like a_b

@16pxdesign
Copy link

Not work well with urls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment