Search code examples
pythonjsonpython-3.xdictionaryrecursion

Reformatting a json tree using python


I have a json tree at hand, I want to reformat it.

**The input is somewhat like : **

[
   {
      "archive":{
         "LOGS":{
            "_files":[
               "zomp_download_20220601170001.log",
               "zomp_download_20220626170002.log"
            ]
         },
         "SUMMARY":{
            "_files":[
               "SUMMARY_zomp_report_2022-04-01.csv",
               "SUMMARY_zomp_report_2022-06-25.csv"
            ]
         },
         "_files":[
            "lexter_report_2022-04-01.csv",
            "lexter_report_2022-06-25.csv"
         ]
      }
   }
]

**and the expected output : **

[
   {
      "text": "archive",
      "nodes":[
         {
            "text":"LOGS",
            "nodes":[
               {
                  "text":"_files",
                  "nodes":[
                     {
                        "text":"zomp_download_20220601170001.log"
                     },
                     {
                        "text":"zomp_download_20220626170002.log"
                     }
                  ]
               }
            ]
         },
         {
            "text": "SUMMARY",
            "nodes":[
               {
                  "text":"SUMMARY_zomp_report_2022-04-01.csv"
               },
               {
                  "text":"SUMMARY_zomp_report_2022-06-25.csv"
               }
            ]
         },
         {
            "text":"lexter_report_2022-04-01.csv"
         },
         {
            "text":"lexter_report_2022-06-25.csv"
         }
      ]
   }
]

I tried to iterate over it, but I am unable to think of any logic to do so. I tried to put the input in a file and read it as a json.

import json

f = open(file)
config_file = json.load(f)

def traverse_dict(d):
   for d_key in d.keys():
    if isinstance(d[d_key], dict):
        new_dict = {}
        new_dict["node"] = [d[d_key]]
        traverse_dict(d[d_key])
    else:
        new_dict = {}
        new_dict["text"] = d[d_key]
 
for i in config_file:
    traverse_dict(i)
    print(i)

I need help in python to convert the json input as the json output. Any help/indicative logic/ examples will be very helpful to me.


Solution

  • You can make a recursive function to achieve your goal :

    import json
    def convert_to_tree(node):
        if isinstance(node, dict):
            return [{"text": key, "nodes": convert_to_tree(value)} for key, value in node.items()]
        elif isinstance(node, list):
            result = []
            for item in node:
                result.extend(convert_to_tree(item))
            return result
        else:
            return [{"text": node}]
    
    def main():
        input_json = [
            {
                "archive": {
                    "LOGS": {
                        "_files": ["zomp_download_20220601170001.log", "zomp_download_20220626170002.log"]
                    },
                    "SUMMARY": {
                        "_files": ["SUMMARY_zomp_report_2022-04-01.csv", "SUMMARY_zomp_report_2022-06-25.csv"]
                    },
                    "_files": ["lexter_report_2022-04-01.csv", "lexter_report_2022-06-25.csv"]
                }
            }
        ]
    
        output_json = convert_to_tree(input_json)
        print(json.dumps(output_json, indent=3))
    
    if __name__ == "__main__":
        main()