Last active
November 22, 2022 09:49
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_string.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":'); | |
eval('var json = new Object(' + json_string + ')'); |
/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9_]+)(['"])?:/
for keys contain underscore
like a_b
Not work well with urls
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to deal with object that contains dot in key?