Home > Software engineering >  Recursive json to csv with python
Recursive json to csv with python

Time:03-11

I have a json file to convert it in json but the case here is the nested json structure:

[ { "node":[ { "node":[ { "node":[ { "node":[ { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Industry is in bank exclusion list" ] }, { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Borrower is currently under bankruptcy law" ] }, { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Borrower is flagged as Unwilling" ] }, { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Borrower is flagged as non-viable" ] }, { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Borrower has blocked access of bank to Tiresias" ] }, { "valBool":false, "valStr1":[ "true" ], "valStr2":[ "Borrower is default (NPE/NPF eba status) " ] }, { "valBool":true, "valStr1":[ "false" ], "valStr2":[ "Default value" ] } ] } ] }, { "node":[ { "node":[ { "node":[ { "node":[ { "node":[ { "valBool":false, "valStr1":[ "1" ], "valStr2":[ "There are less \nthan 10 employees" ] }, { "valBool":false, "valStr1":[ "1" ], "valStr2":[ "Annual turnover is \nlower than annual \nturnover threshold" ] }, { "valBool":false, "valStr1":[ "1" ], "valStr2":[ "Total assets are \nlower than total \nassets threshold" ] }, { "valBool":true, "valStr1":[ "0" ], "valStr2":[ "Default" ] } ] } ] } ] } ] } ] } ] } ] } ]

As you can see 'node' can be find at any level. I tried some recursive aproaches but the output is not what we want. We need to take every node that has the three values inside and write them to one line on csv.

The expected output should be:

valBool,valStr1,valStr2
false,"true","Industry is in bank exclusion list"
false,"true","Borrower is currently under bankruptcy law"

I have tried this but the output justs appends each value in new line writing the whole path on key.

Any ideas?

Thank you!

CodePudding user response:

You have to consider in your recursive function if the data you're processing is a list or a dictionary. If it's a list you simply recusively call your function on its items. If it's a dictionary you try to print values associated to 'valBool', 'valStr1' and 'valStr2' if they are present and recursively call on the value associated to 'node' if any.

data = [ { "node":[ { "node":[ { "node":[ { "node":[ { "valBool":False, "valStr1":[ "true" ], "valStr2":[ "Industry is in bank exclusion list" ] }, { "valBool":False, "valStr1":[ "true" ], "valStr2":[ "Borrower is currently under bankruptcy law" ] }, { "valBool": False, "valStr1":[ "true" ], "valStr2":[ "Borrower is flagged as Unwilling" ] }, { "valBool": False, "valStr1":[ "true" ], "valStr2":[ "Borrower is flagged as non-viable" ] }, { "valBool": False, "valStr1":[ "true" ], "valStr2":[ "Borrower has blocked access of bank to Tiresias" ] }, { "valBool":False, "valStr1":[ "true" ], "valStr2":[ "Borrower is default (NPE/NPF eba status) " ] }, { "valBool":True, "valStr1":[ "false" ], "valStr2":[ "Default value" ] } ] } ] }, { "node":[ { "node":[ { "node":[ { "node":[ { "node":[ { "valBool":False, "valStr1":[ "1" ], "valStr2":[ "There are less \nthan 10 employees" ] }, { "valBool":False, "valStr1":[ "1" ], "valStr2":[ "Annual turnover is \nlower than annual \nturnover threshold" ] }, { "valBool":False, "valStr1":[ "1" ], "valStr2":[ "Total assets are \nlower than total \nassets threshold" ] }, { "valBool":True, "valStr1":[ "0" ], "valStr2":[ "Default" ] } ] } ] } ] } ] } ] } ] } ] } ]

def loop(data):
    if isinstance(data, list):
        for item in data:  # data is a list => recursive call on all its items
            loop(item)
    elif isinstance(data, dict):  # data is a dictionary
        try:
            print(
                f"{data['valBool']};{data['valStr1'][0]};{data['valStr2'][0]}"
            )
        except KeyError: # dictionary does not have all valXXX keys
            pass
        if 'node' in data:  # recursive call if the dictionary has a "node" key
            loop(data['node'])

print('valBool;valStr1;valStr2')
loop(data)

It's not exactly the output you're expecting, but you'll figure out how to modify it.

  • Related