I had a requirement to convert a JSON response into a csv file. I was able to successfully use Tim Yates' excellent code from here: Groovy code to convert json to CSV file
I now need to include the JSON's nested submap in the csv as well. The relationship between the map and submap is 1:1.
I've been unable to get the correct syntax for a collect statement that will retrieve both the parsed map and submap key/values.
Sample JSON
{items=
[
{
created_at=2019-03-27
, entity_id=1
, extension_attributes=[]
},
{
created_at=2019-03-27
, entity_id=2
, extension_attributes= { employee_id=Emp1, employee_type=CSR}//nested submap
}
]}
Groovy
import groovy.json.*
def data = new JsonSlurper().parseText( json ); //"json" is from GET request
def columns = ["created_at","entity_id","employee_id","employee_type"]
def encode = { e -> e ? /"$e"/ : "$e"}
requestFile.append( columns.collect { c -> encode( c ) }.join( ',' ) + '\n');
requestFile.append( data.items.collect { row ->columns.collect { colName -> encode( row[ colName ] ).replaceAll("null","") }.join( ',' )}.join( '\n' ) );//unsure how to use data.items.collect to fetch submap
I would like to either 1) Convert the JSON as follows to collect each key/value easily:
...
{
created_at=2019-03-27
, entity_id=2
, employee_id=Emp1
, employee_type=CSR
}
...
or 2) Find out if there's a way to use Groovy's collect method to retrieve the map/submap as a flat map.
I am unfortunately not a programmer by trade, and any help would be appreciated!