I've got some JSON data being returned from a REST call that I want to parse, add totals, then spit out arrays with new data. I've got the parsing, looping, and adding figured out, and can write the results on the page (see post: json sibling data), but I want to further break down the totals. Here's the JSON I'm starting with:
{"ResultSet":{ "Result":[ { "file_size":"722694", "desc":"description1", "format":"GIF" }, { "file_size":"19754932", "desc":"description1", "format":"JPEG" }, { "file_size":"778174", "desc":"description2", "format":"GIF" }, { "file_size":"244569996", "desc":"description1", "format":"PNG" }, { "file_size":"466918", "desc":"description2", "format":"TIFF" } ] }}
I've got it returning the totals for each different "desc" (see answer: https://stackoverflow.com/a/13016615/1766026), but now I want to break it down one step further and show totals for each "desc" with each "format" so the new output would look like:
description1: 444MB (222MB TIFF, 111MB GIF, 111MB JPEG)
description2: 333MB (111MB PNG, 111MB TIFF, 111MB JPEG)
where not all returned items have the same kinds of file formats.
(yes, I know these numbers do not add up from the JSON - it's just an example)
I'm thinking this can be done by pushing the results to a new array(s) based on matching elements then iterating over that and spitting out to the page.
Maybe the new array(s)/object(s) would look like this?
{ "desc":"description1", "TIFF":"222", "GIF:"111", "JPEG:"111" }, { "desc":"description2", "PNG":"111", "TIFF:"111", "JPEG":"111" }
I just saw this: How do I create JavaScript array (JSON format) dynamically? I guess that would be one place to start?
(please pardon possible improper terminology - I do mostly front-end work and this kind of stuff is pretty new to me - polite constructive criticisms gladly accepted)