Skip to content

Instantly share code, notes, and snippets.

@manrueda manrueda/converter.js
Last active Aug 24, 2016

Embed
What would you like to do?
Convert a JSON string to a an string with JS code representing that object
(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
You can’t perform that action at this time.