1

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!

NScara
  • 55
  • 6

2 Answers2

1

Here is the flatten closure which flattens an item recursively:

def flatten
flatten = { row ->
    def flattened = [:]
    row.each { k, v ->
        if (v instanceof Map) {
            flattened << flatten(v)
        } else {
            flattened[k] = v
        }
    }
    flattened
}

You should just replace row with flatten(row) at your last line so it looks like this:

requestFile.append(data.items.collect { row ->
    columns.collect {
        colName -> encode(flatten(row)[colName]).replaceAll("null", "")
    }.join(',')
}.join('\n'))

The result will be as follows:

"created_at","entity_id","employee_id","employee_type"

"2019-03-27","1",,
"2019-03-27","2","Emp1","CSR"
Dmitry Khamitov
  • 3,061
  • 13
  • 21
  • Thank you Dmitry! Was able to successfully test flatten closure and results were as expected. – NScara Apr 04 '19 at 18:05
1

Also found that following allows for collect method to fetch nested elements:

def m = data.items.collect{[/"${it?.created_at?:''}"/,/"${it?.extension_attributes?.entity_id?:''}"/,/"${it?.extension_attributes?.employee_id?:''}"/,/"${it?.extension_attributes?.employee_type?:''}"/]}

m.each{requestFile.append(it.join(',')+'\n')}
NScara
  • 55
  • 6