Home > OS >  converting an unordered nested json into a specific format
converting an unordered nested json into a specific format

Time:04-07

I have a json like this :

[
  {
    "a": 1,
    "b": 2,
    "c": 3,
    "d": 4,
    "more": { "sd": 2, "fg": 5, "ef": 6 },
    "s": 6,
    "as": 8,
    "dfd": [{ "sda": "something", "SX": ["CB"], "jhgjh": "", "das": "" }]
  },
  {
    "a3": 1,
    "b": 2,
    "c": 3,
    "ds": 4,
    "more": { "sd": 32, "fg": 5, "3ef": 6 },
    "s": 6,
    "as": 38,
    "dfd": [
      { "sda": "somethinDDDg", "SX": ["CDB"], "jhgjh": "1", "das": "wes" }
    ]
  }
]

I am trying to loop over all levels and convert the json into a specific format. I json can have any number of key value pairs and levels. Key value pairs can be different for each json entry. I tried using for loop on key value pair and was able to get it converted into the required format on first level of json. I am unable to expand it to any level (where level is not fixed). Expected output :

    'data' : [{
    {
    'name' : 'a',
    'value':1
    },
    {
    'name' : 'b',
    'value': 2
    },
    {
    'name' : 'c',
    'value': 3
    },
    {
    'name' : 'd',
    'value': 4
    },
    {
    'name' : 'more',
    'value': {'name' :'sd',
          'value': 2},
        {'name' : 'fg',
        'value' : 5},
        {'name':'ef',
        'value':6},
    {
    'name' : 's',
    'value': 6
    },
    {
    'name' : 'as',
    'value': 
    },
    {
    'name' : 'dfd',
    'value': [  {"sda": "something",
               "SX":["CB"],
                "jhgjh": "",
                "das": ''
               }]
    },
    
{{
    'name' : 'a3',
    'value':1
    },
    {
    'name' : 'b',
    'value': 2
    },
    {
    'name' : 'c',
    'value': 3
    },
    {
    'name' : 'ds',
    'value': 4
    },
    {
    'name' : 'more',
    'value': {'name' :'sd',
          'value': 32},
        {'name' : 'fg',
        'value' : 5},
        {'name':'3ef',
        'value':6},
    {
    'name' : 's',
    'value': 6
    },
    {
    'name' : 'as',
    'value': 38
    },
    {
    'name' : 'dfd',
    'value': [  {"sda": "somethinDDDg",
               "SX":["CDB"],
                "jhgjh": "1",
                "das": 'wes'
               }]
    
    
    }}]
     

CodePudding user response:

Okay, I think I understand now what you want. Using a recursive helper function:

def names_and_values(d):
    if type(d) != dict:
        return d
    return [{"name": key, "value": names_and_values(value)} for key, value in d.items()]

And the json module from the standard library:

import json

data = json.loads(your_json_string)
converted_data = [names_and_values(d) for d in data]

Output:

[[{'name': 'a', 'value': 1},
  {'name': 'b', 'value': 2},
  {'name': 'c', 'value': 3},
  {'name': 'd', 'value': 4},
  {'name': 'more',
   'value': [{'name': 'sd', 'value': 2},
             {'name': 'fg', 'value': 5},
             {'name': 'ef', 'value': 6}]},
  {'name': 's', 'value': 6},
  {'name': 'as', 'value': 8},
  {'name': 'dfd',
   'value': [{'sda': 'something',
              'SX': ['CB'],
              'jhgjh': '',
              'das': ''}]}],
 [{'name': 'a3', 'value': 1},
  {'name': 'b', 'value': 2},
  {'name': 'c', 'value': 3},
  {'name': 'ds', 'value': 4},
  {'name': 'more',
   'value': [{'name': 'sd', 'value': 32},
             {'name': 'fg', 'value': 5},
             {'name': '3ef', 'value': 6}]},
  {'name': 's', 'value': 6},
  {'name': 'as', 'value': 38},
  {'name': 'dfd',
   'value': [{'sda': 'somethinDDDg',
              'SX': ['CDB'],
              'jhgjh': '1',
              'das': 'wes'}]}]]
  • Related