Skip to content

Instantly share code, notes, and snippets.

@agrublev
Forked from larruda/unquoted_json_fix.js
Last active April 5, 2022 09:38
Show Gist options
  • Save agrublev/920765a1ab4a0ea05a47b319fc8ca8a0 to your computer and use it in GitHub Desktop.
Save agrublev/920765a1ab4a0ea05a47b319fc8ca8a0 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). fix json string
const fixJsonStr=str=>str.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '$1"$3":');
let zzzz=fixJsonStr(`{
"kettty": "",
asds: [], kggk:{},
"ga": 222,
"sad": {
"zz": 0,
"as": false,
"gag": []
},
"zztt": false
}`);
console.log(JSON.parse(zzzz))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment