Skip to content

Instantly share code, notes, and snippets.

@dylansmith
Created January 11, 2016 09:25
Show Gist options
  • Save dylansmith/2d3e7fe2df256f0e6064 to your computer and use it in GitHub Desktop.
Save dylansmith/2d3e7fe2df256f0e6064 to your computer and use it in GitHub Desktop.
Recursively transform JSON keys
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