0

I have a dictionary like this:

[
  {
    "context": {
      "id": "aaa",
      "number": "123456",
      "codes": {
        "code": {
          "code": "abc123456",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "abc123456"
    },
    "spec": {
      "code": "abc123456",
      "id": "aaa",
      "url": "https://myexampleabc.mydomain.com",
      "enable": true
    }
  },
  {
    "context": {
      "id": "bbb",
      "number": "789123",
      "codes": {
        "code": {
          "code": "def789123",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "def789123"
    },
    "spec": {
      "code": "def789123",
      "id": "bbb",
      "url": "https://myexampledef.mydomain.com",
      "enable": true
    }
  },
  {
    "context": {
      "id": "ccc",
      "number": "456789",
      "codes": {
        "code": {
          "code": "ghi456789",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "ghi456789"
    },
    "spec": {
      "code": "ghi456789",
      "id": "ccc",
      "url": "https://myexampleghi.mydomain.com",
      "enable": true
    }
  }
]

I want to translate it like this: "key1.key2.key3" : "value"

[
  {
    "context.id": "aaa",
    "context.number": "123456",
    "context.codes.code.code": "abc123456",
    "context.codes.code.codeDesc": "sample",
    "context.codes.code.codeInfo": "example",
    "info.action": "CREATE",
    "info.code": null,
    "info.status": "SUCCESS",
    "info.description": "abc123456",
    "spec.code": "abc123456",
    "spec.id": "aaa",
    "spec.url": "https://myexampleabc.mydomain.com",
    "spec.enable": true
  },
  {
    "context.id": "bbb",
    "context.number": "789123",
    "context.codes.code.code": "def789123",
    "context.codes.code.codeDesc": "sample",
    "context.codes.code.codeInfo": "example",
    "info.action": "CREATE",
    "info.code": null,
    "info.status": "SUCCESS",
    "info.description": "def789123",
    "spec.code": "def789123",
    "spec.id": "bbb",
    "spec.url": "https://myexampledef.mydomain.com",
    "spec.enable": true
  },
  {
    "context.id": "ccc",
    "context.number": "456789",
    "context.codes.code.code": "ghi456789",
    "context.codes.code.codeDesc": "sample",
    "context.codes.code.codeInfo": "example",
    "info.action": "CREATE",
    "info.code": null,
    "info.status": "SUCCESS",
    "info.description": "ghi456789",
    "spec.code": "ghi456789",
    "spec.id": "ccc",
    "spec.url": "https://myexampleghi.mydomain.com",
    "spec.enable": true
  }
]

I try to do it with some loop and isinstance() function, but i can't do what i want. I'm not abble to handle the sublevel for code.

I try something like this

jsondata = json.loads(dataJson)

for level0 in jsondata:
    if isinstance(level0, dict):
      level1Keys = level0.keys()
      level1Values = level0.values()
      print(level1Keys)
      print(level1Values)

I've also tried the .pop() method, but I think I'm not in the right way to do it...

3
  • null and true are not Python values. Pls correct that, so helpers can copy and paste that. Commented Aug 25, 2021 at 12:22
  • This should help you stackoverflow.com/questions/6027558/… Commented Aug 25, 2021 at 12:29
  • I think you will need to be more clear with the exact final dataframe form you're aiming for. If you could do that, it would be great. Commented Aug 25, 2021 at 12:29

4 Answers 4

1

The following recursive function falttens a nested dict to dotted path keys:

def flatten(obj):
    if isinstance(obj, dict):
        res = {}
        for k, v in obj.items():
            f = flatten(v)
            if isinstance(f, dict):
                for k_, v_ in f.items():
                    res[f"{k}.{k_}"] = v_
            else:
                res[k] = f
        return res
    return obj

flattened = list(map(flatten, jsondata))
Sign up to request clarification or add additional context in comments.

Comments

0

you can try something like this

l = json.loads("""[
  {
    "context": {
      "id": "aaa",
      "number": "123456",
      "codes": {
        "code": {
          "code": "abc123456",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "abc123456"
    },
    "spec": {
      "code": "abc123456",
      "id": "aaa",
      "url": "https://myexampleabc.mydomain.com",
      "enable": true
    }
  },
  {
    "context": {
      "id": "bbb",
      "number": "789123",
      "codes": {
        "code": {
          "code": "def789123",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "def789123"
    },
    "spec": {
      "code": "def789123",
      "id": "bbb",
      "url": "https://myexampledef.mydomain.com",
      "enable": true
    }
  },
  {
    "context": {
      "id": "ccc",
      "number": "456789",
      "codes": {
        "code": {
          "code": "ghi456789",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": null,
      "status": "SUCCESS",
      "description": "ghi456789"
    },
    "spec": {
      "code": "ghi456789",
      "id": "ccc",
      "url": "https://myexampleghi.mydomain.com",
      "enable": true
    }
  }
]""")

def flatten_dict(d, current_key, final_dict):
    for k,v in d.items():
        if isinstance(v, dict):
            current_key.append(k)
            flatten_dict(v, current_key, final_dict)
            current_key.pop()
        else:
            current_key.append(k)
            final_dict['.'.join(current_key)] = v
            current_key.pop()
    return final_dict

result = [flatten_dict(i, [], {}) for i in l]
[{'context.id': 'aaa',
  'context.number': '123456',
  'context.codes.code.code': 'abc123456',
  'context.codes.code.codeDesc': 'sample',
  'context.codes.code.codeInfo': 'example',
  'info.action': 'CREATE',
  'info.code': None,
  'info.status': 'SUCCESS',
  'info.description': 'abc123456',
  'spec.code': 'abc123456',
  'spec.id': 'aaa',
  'spec.url': 'https://myexampleabc.mydomain.com',
  'spec.enable': True},
 {'context.id': 'bbb',
  'context.number': '789123',
  'context.codes.code.code': 'def789123',
  'context.codes.code.codeDesc': 'sample',
  'context.codes.code.codeInfo': 'example',
  'info.action': 'CREATE',
  'info.code': None,
  'info.status': 'SUCCESS',
  'info.description': 'def789123',
  'spec.code': 'def789123',
  'spec.id': 'bbb',
  'spec.url': 'https://myexampledef.mydomain.com',
  'spec.enable': True},
 {'context.id': 'ccc',
  'context.number': '456789',
  'context.codes.code.code': 'ghi456789',
  'context.codes.code.codeDesc': 'sample',
  'context.codes.code.codeInfo': 'example',
  'info.action': 'CREATE',
  'info.code': None,
  'info.status': 'SUCCESS',
  'info.description': 'ghi456789',
  'spec.code': 'ghi456789',
  'spec.id': 'ccc',
  'spec.url': 'https://myexampleghi.mydomain.com',
  'spec.enable': True}]

1 Comment

@LeRoyLoïc actually there was a major flaw in my code regarding shallow copy, please make sure you checked the new code. It is very important. And I really apologize for not testing my code before posting.
0

You can try recursion:

lst = [
  {
    "context": {
      "id": "aaa",
      "number": "123456",
      "codes": {
        "code": {
          "code": "abc123456",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": None,
      "status": "SUCCESS",
      "description": "abc123456"
    },
    "spec": {
      "code": "abc123456",
      "id": "aaa",
      "url": "https://myexampleabc.mydomain.com",
      "enable": True
    }
  },
  {
    "context": {
      "id": "bbb",
      "number": "789123",
      "codes": {
        "code": {
          "code": "def789123",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": None,
      "status": "SUCCESS",
      "description": "def789123"
    },
    "spec": {
      "code": "def789123",
      "id": "bbb",
      "url": "https://myexampledef.mydomain.com",
      "enable": True
    }
  },
  {
    "context": {
      "id": "ccc",
      "number": "456789",
      "codes": {
        "code": {
          "code": "ghi456789",
          "codeDesc": "sample",
          "codeInfo": "example"
        }
      }
    },
    "info": {
      "action": "CREATE",
      "code": None,
      "status": "SUCCESS",
      "description": "ghi456789"
    },
    "spec": {
      "code": "ghi456789",
      "id": "ccc",
      "url": "https://myexampleghi.mydomain.com",
      "enable": True
    }
  }
]

def join_keys(d, res={}):
    for k, v in d.items():
        if isinstance(v, dict):
            for key, val in v.copy().items():
                del v[key]
                v[f"{k}.{key}"] = val
                if isinstance(val, str):
                    res[f"{k}.{key}"] = val
            join_keys(v, res)
    return res

for d in lst:
    print(join_keys(d))

Output:

{"context.id": "aaa",
 "context.number": "123456",
 "context.codes.code.code": "abc123456",
 "context.codes.code.codeDesc": "sample",
 "context.codes.code.codeInfo": "example",
 "info.action": "CREATE",
 "info.code": None,
 "info.status": "SUCCESS",
 "info.description": "abc123456",
 "spec.code": "abc123456",
 "spec.id": "aaa",
 "spec.url": "https://myexampleabc.mydomain.com",
 "spec.enable": True}
{"context.id": "bbb",
 "context.number": "789123",
 "context.codes.code.code": "def789123",
 "context.codes.code.codeDesc": "sample",
 "context.codes.code.codeInfo": "example",
 "info.action": "CREATE",
 "info.code": None,
 "info.status": "SUCCESS",
 "info.description": "def789123",
 "spec.code": "def789123",
 "spec.id": "bbb",
 "spec.url": "https://myexampledef.mydomain.com",
 "spec.enable": True}
{"context.id": "ccc",
 "context.number": "456789",
 "context.codes.code.code": "ghi456789",
 "context.codes.code.codeDesc": "sample",
 "context.codes.code.codeInfo": "example",
 "info.action": "CREATE",
 "info.code": None,
 "info.status": "SUCCESS",
 "info.description": "ghi456789",
 "spec.code": "ghi456789",
 "spec.id": "ccc",
 "spec.url": "https://myexampleghi.mydomain.com",
 "spec.enable": True}

Comments

0

you can try this a snipped way of obtain info, i hope helps your.

level=[]
data={} 
for fields in some_dict :
  data["context.id"] =  fields.get("context").get("id")
  data["context.number"] =  fields.get("context").get("number")
  data["context.codes"] =  fields.get("context").get("codes").get("code")
  data["context.codes.codeDesc"] = fields.get("context").get("codes").get("codeDesc")
  data["context.codes.codeInfo"] =  fields.get("context").get("codes").get("codeInfo")
  data["info.action"] = fields.get("info").get("action")
  level.append(data)
  print(level)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.