Last active
August 24, 2016 14:59
-
-
Save manrueda/dd98fd7b5919ff83e146a5a2ac79f491 to your computer and use it in GitHub Desktop.
Convert a JSON string to a an string with JS code representing that object
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
(JSONString => { | |
let objectString = JSONString; | |
JSONString.match(/"(.*?)":\n*(.*)/gi).forEach(line => { | |
var parts = line.match(/"(.*?)":\n*(.*)/); | |
if (parts[2].match(/^"(.*)",?$/)){ | |
let hasComma = !!parts[2].match(/^"(.*)",{1}$/) | |
parts[2] = '\'' + parts[2].match(/^"(.*)",?$/)[1] + '\'' + (hasComma ? ',': ''); | |
} | |
objectString = objectString.replace(parts[0], parts[1] + ': ' + parts[2]) | |
}) | |
return objectString; | |
})(JSONString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment