Skip to content

Instantly share code, notes, and snippets.

@dlindahl
Created July 10, 2014 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlindahl/4b9184a727d48925463e to your computer and use it in GitHub Desktop.
Save dlindahl/4b9184a727d48925463e to your computer and use it in GitHub Desktop.
City of Sacramento API Response Transform
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);
@dlindahl
Copy link
Author

This will transform the one-giant-array "JSON" response into an actual JSON response.

This returns an array of objects:

{ Name: '133770',
    Latitude: '38.57596583572064',
    Longitude: '-121.4846154480373',
    Altitude: '0.0',
    Description: '<html xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt"><head><META http-equiv="Content-Type" content="text/html"><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body style="margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;"><table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px"><tr style="text-align:center;font-weight:bold;background:#9CBCE2"><td>133770</td></tr><tr><td><table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px"><tr><td>GISOBJID</td><td>100049235</td></tr><tr bgcolor="#D4E4F3"><td>OBJ_CODE</td><td>133770</td></tr><tr><td>ADDRESS_NUMBER</td><td>1701</td></tr><tr bgcolor="#D4E4F3"><td>STREET</td><td>L ST</td></tr><tr><td>LANDUSE</td><td>Other</td></tr><tr bgcolor="#D4E4F3"><td>PLANTTYPE</td><td>Tree Well</td></tr><tr><td>GROWSPACE</td><td>6-7ft</td></tr><tr bgcolor="#D4E4F3"><td>CONDUCTOR</td><td>&lt;Null&gt;</td></tr><tr><td>HDSCAPE</td><td>&lt;Null&gt;</td></tr><tr bgcolor="#D4E4F3"><td>SPECIES</td><td>London plane tree</td></tr><tr><td>BOTANICAL</td><td>Platanus X acerifolia</td></tr><tr bgcolor="#D4E4F3"><td>CULTIVAR</td><td>&lt;Null&gt;</td></tr><tr><td>STEMS</td><td>&lt;Null&gt;</td></tr><tr bgcolor="#D4E4F3"><td>DBH</td><td>25 to 36</td></tr><tr><td>ROOT</td><td>&lt;Null&gt;</td></tr><tr bgcolor="#D4E4F3"><td>WOOD</td><td>&lt;Null&gt;</td></tr><tr><td>FOLIAGE</td><td>&lt;Null&gt;</td></tr><tr bgcolor="#D4E4F3"><td>SHAPE</td><td>Point</td></tr><tr><td>SHAPE_FID</td><td>100376</td></tr><tr bgcolor="#D4E4F3"><td>POINT_X</td><td>-121.484615</td></tr><tr><td>POINT_Y</td><td>38.575966</td></tr></table></td></tr></table></body></html>' }

It is completely agnostic as to what the data looks like, as long as the header count matches the column count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment