Skip to content

News

Organizing JSON output

Jun 24, 2011

There might be something to say for defining interfaces up front. Spending over one day cleaning up and re-organizing JSON output throughout multiple subprojects can be quite tedious. Once again, sed proved helpful, but sometimes it's harder to make sed perform like the scalpel than to run a quick grep search and manually edit.

Despite the tedium, it's nice to end up with a cleaner, compliant interface. These are a few of the things fixed:

  • Convert dashes in element names to underscores
  • Remove integer index references in element names, convert to arrays
  • Group common objects into JSON objects
  • Write number and boolean elements as such, not as strings

 
 
 
 
Along with the JSON object group and array changes, I added some javascript on the web page to parse the objects and arrays into the associated HTML form element names. I did this by recursively traversing the JSON object and concatenating children names with an underscore. For arrays, I simply appended an underscore and the element index. The name that was built mapped to an HTML form element that was auto-populated with the returned value.