Created
July 10, 2014 06:13
-
-
Save dlindahl/4b9184a727d48925463e to your computer and use it in GitHub Desktop.
City of Sacramento API Response Transform
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
var _ = require('lodash'); | |
var data = require('./data.json'); | |
// Retrieve the header names | |
var headers = _(data.result.fArray).where({fHeader:true}).pluck('fStr').value(); | |
// Transform the data | |
var items = | |
_(data.result.fArray) | |
.rest(headers.length) // Drop the header items | |
.groupBy(function(item, idx) { // Convert the long list of items into succint objects | |
return Math.floor(idx/headers.length); | |
}) | |
.toArray() // Convert the group back into an array | |
.map(function(item, idx) { // Pull the values from the dataset and merge w/ the headers | |
return _.zipObject(headers, _(item).flatten().pluck('fStr').value()); | |
}) | |
.value(); // end the lodash chain | |
console.log(items); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will transform the one-giant-array "JSON" response into an actual JSON response.
This returns an array of objects:
It is completely agnostic as to what the data looks like, as long as the header count matches the column count.