skip to Main Content

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.

2

Answers


  1. 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()
    
    
    
    
    
    Login or Signup to reply.
  2. I think this simple 1:1 transformation should do it –

    import json
    
    def remap(t):
      if isinstance(t, dict): 
        for k, v in t.items():
          yield {"text": k, "nodes": list(remap(v))}
      elif isinstance(t, list):
        for v in t:
          yield from remap(v)
      elif isinstance(t, str):
        yield {"text": t}
      else:
        raise TypeError(f"Unsupported type: {t}")
    
    input = """
    [
       {
          "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"
             ]
          }
       }
    ]
    """
    

    Decode the JSON, remap it, encode it, print it –

    print(json.dumps(list(remap(json.loads(input))), indent=2))
    

    The remapped output –

    [
      {
        "text": "archive",
        "nodes": [
          {
            "text": "LOGS",
            "nodes": [
              {
                "text": "_files",
                "nodes": [
                  {
                    "text": "zomp_download_20220601170001.log"
                  },
                  {
                    "text": "zomp_download_20220626170002.log"
                  }
                ]
              }
            ]
          },
          {
            "text": "SUMMARY",
            "nodes": [
              {
                "text": "_files",
                "nodes": [
                  {
                    "text": "SUMMARY_zomp_report_2022-04-01.csv"
                  },
                  {
                    "text": "SUMMARY_zomp_report_2022-06-25.csv"
                  }
                ]
              }
            ]
          },
          {
            "text": "_files",
            "nodes": [
              {
                "text": "lexter_report_2022-04-01.csv"
              },
              {
                "text": "lexter_report_2022-06-25.csv"
              }
            ]
          }
        ]
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search