Created
January 11, 2016 09:25
-
-
Save dylansmith/2d3e7fe2df256f0e6064 to your computer and use it in GitHub Desktop.
Recursively transform JSON keys
This file contains hidden or 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
var fs = require('fs') | |
var _ = require('lodash') | |
var humps = require('humps') | |
var input = require('./input.json') | |
var outputFile = './output.json' | |
function processObj(val) { | |
if (_.isArray(val)) { | |
return val.map(item => processObj(item)) | |
} | |
if (_.isObject(val)) { | |
val = _.mapKeys(val, (val, key) => humps.camelize(key)) | |
for (k in val) { | |
val[k] = processObj(val[k]) | |
} | |
return val | |
} | |
return val | |
} | |
var output = JSON.stringify(processObj(input), null, 2) | |
fs.writeFileSync(outputFile, output, 'utf8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment