Dynamic Conversion of JSON to Table Sequence
【Question】
I have a problem very similar to
http://community.jaspersoft.com/questions/844023/need-help-looping-throu…,
But I can’t just change the JSON file as done by the person asking that question.
Any help would be greatly appreciated.
----
I am trying to create a report in iReport Designer v5.6.0 that loops through a JSON file and returns all data from different levels. For example, I’ve built a very simple JSON file that has a list of customers, and for each customer it lists all of his/her vehicles including fields for make, model and mileage. The desired output would be a list of customers and underneath each customer a list of the vehicles he/she owns.
I have built a template that correctly displays each customer name, but I have been unable to figure out how to loop through each customer’s vehicles. (I can return the first vehicle by adding [0] to the field, but I can’t figure out how to loop through all vehicles).
JSON data:
[{“custName”: “Alison Anderson”,“custCars”:[{“carMake”: “Chevrolet”,“carModel”: “Avalanche”,“carMiles”: 97740},{“carMake”: “Chevrolet”,“carModel”: “Beretta”,“carMiles”: 165500}]},{“custName”: “Bart Bloomfield”,“custCars”:[{“carMake”: “Ford”,“carModel”: “F150”,“carMiles”: 43252},{“carMake”: “Mercury”,“carModel”: “Marauder”,“carMiles”: 26699}]},{“custName”: “Charles Cominsky”,“custCars”:[{“carMake”: “Porsche”,“carModel”: “Cayman”,“carMiles”: 15520}]}]
【Answer】
It’s more convenient to convert the multilevel JSON data to a two-dimensional table if we can prepare the JSON data into the desired format in an independent computing tool. Here I prepare it with esProc. esProc is such a tool driven by Structured Process Language (SPL). Below is the SPL script:
A |
|
1 |
=json(file(“D:/cars.json”).read()) |
2 |
=A1.news(custCars;A1.custName:custName,carMake,carModel,carMiles) |
A1: Read in the JSON data as a text file and then into a table sequence. Here A1 is equivalent to a target result table grouped by custName field.
A2: Based on A1’s table sequence, news() function parses values under custCars field to form a new table sequence and joins each record after A1’s custName.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL