I have a groovy script that converts flat file CSV with Headers, Line items and Footer into a JSON. I am using a simple code as shown below but its not giving me the desired output. My expectation is that line items should come under each header and footer should be at the same level as the Header node and it should come at the end.
Please help me out in this regard.
GROOVY CODE:
def lines = '''
H 01 FATXXX 08012111 08012000 1549999 CAD
I 1545093 01.01.074.HBNY.002 D 71.34
I 1545093 01.01.HDK.1240 C 71.34
F 2 71.34 71.34
'''.split('n')
def json = [:]
def items = []
lines.each { line ->
def parts = line.split('t')
def type = parts[0]
if (type == 'H') {
json['header'] = [
'CompanyCode': parts[1],
'JournalEntryType': parts[2],
'PostingDate': parts[3],
'JournalEntryDate': parts[4],
'ReferenceDocNumber': parts[5],
'TransCurr': parts[6],
'DocText': parts[7]
]
} else if (type == 'I') {
items << [
'ReferenceDocNumber': parts[1],
'GL_Account': parts[2],
'Cost_Center': parts[3],
'Profit_Center': parts[4],
'WBS_Element': parts[5],
'D_C_Indicator': parts[6],
'Item_Text': parts[7],
'Assignment': parts[8],
'AmountinTransactionCurrency': parts[9],
'LocalCurrency': parts[10],
'AmountinLocalCurrency': parts[11],
'GroupCurrency': parts[12],
'AmountinGroupCurrency': parts[13],
'ParallelCurrency': parts[14],
'AmountinParallelCurrency': parts[15],
'TransactionType': parts[16],
'HouseBank': parts[17],
'BankAccountID': parts[18],
'ValueDate': parts[19],
'TradingPartner': parts[20],
'IFF_VENDOR': parts[21],
'IFF_CUSTOMER': parts[22]
]
} else if (type == 'F') {
json['footer'] = [
'Total_Records': parts[1],
'Total_Debit': parts[2],
'Total_Credit': parts[3]
]
}
}
json['items'] = items
def jsonString = JsonOutput.toJson(json)
println(jsonString)
}
EXPECTED OUTPUT:
{
"GL_Data": {
"Headers": [
{
"CompanyCode": 1,
"JournalEntryType": "APPURREG",
"PostingDate": 8302023,
"JournalEntryDate": 8302023,
"ReferenceDocNumber": 493890,
"TransCurr": "CAD",
"DocText": "",
"Items": [
{
"ReferenceDocNumber": 493890,
"GL_Account": "01.00.001.0011",
"Cost_Center": "",
"Profit_Center": "",
"WBS_Element": "",
"D_C_Indicator": "D",
"Item_Text": "",
"Assignment": "",
"AmountinTransactionCurrency": 10,
"LocalCurrency": "",
"AmountinLocalCurrency": "",
"GroupCurrency": "",
"AmountinGroupCurrency": "",
"ParallelCurrency": "",
"HouseBank": "",
"BankAccountID": "",
"TradingPartner": "",
"IFF_VENDOR": "",
"IFF_CUSTOMER": ""
},
{
"ReferenceDocNumber": 493890,
"GL_Account": "01.01.000.0012",
"Cost_Center": "",
"Profit_Center": "",
"WBS_Element": "",
"D_C_Indicator": "C",
"Item_Text": "",
"Assignment": "",
"AmountinTransactionCurrency": 10,
"LocalCurrency": "",
"AmountinLocalCurrency": "",
"GroupCurrency": "",
"AmountinGroupCurrency": "",
"ParallelCurrency": "",
"HouseBank": "",
"BankAccountID": "",
"TradingPartner": "",
"IFF_VENDOR": "",
"IFF_CUSTOMER": ""
}
]
},
{
"CompanyCode": 1,
"JournalEntryType": "APPURREG",
"PostingDate": 8302023,
"JournalEntryDate": 8302023,
"ReferenceDocNumber": 493890,
"TransCurr": "CAD",
"DocText": "",
"Items": [
{
"ReferenceDocNumber": 493890,
"GL_Account": "01.00.001.0001",
"Cost_Center": "",
"Profit_Center": "",
"WBS_Element": "",
"D_C_Indicator": "D",
"Item_Text": "",
"Assignment": "",
"AmountinTransactionCurrency": 10,
"LocalCurrency": "",
"AmountinLocalCurrency": "",
"GroupCurrency": "",
"AmountinGroupCurrency": "",
"ParallelCurrency": "",
"HouseBank": "",
"BankAccountID": "",
"TradingPartner": "",
"IFF_VENDOR": "",
"IFF_CUSTOMER": ""
},
{
"ReferenceDocNumber": 493890,
"GL_Account": "01.01.000.0002",
"Cost_Center": "",
"Profit_Center": "",
"WBS_Element": "",
"D_C_Indicator": "C",
"Item_Text": "",
"Assignment": "",
"AmountinTransactionCurrency": 10,
"LocalCurrency": "",
"AmountinLocalCurrency": "",
"GroupCurrency": "",
"AmountinGroupCurrency": "",
"ParallelCurrency": "",
"HouseBank": "",
"BankAccountID": "",
"TradingPartner": "",
"IFF_VENDOR": "",
"IFF_CUSTOMER": ""
}
]
}
]
}
}
Regards,
Arkesh
2
Answers
I think I understand what you want, but I might be off. Here is what I think you need to do. Change this line:
To this:
You’ll also want to match up the casing in your code with the desired output (ie "headers" -> "Headers" and "footer" -> "Footer") to get what you’re after.
If you have a multi-grouped CSV, you should be using a
list
instead of themap
to collect your entries:You will get the following ouptut, where the
H-I-F
elements are groupped into a single list entry: