Home > Back-end >  Explode json without pandas
Explode json without pandas

Time:12-11

I have a JSON object:

{
  "data": {
    "geography": [
      {
        "id": "1",
        "state": "USA",
        "properties": [
          {
            "code": "CMD-01",
            "value": "34"
          },
          {
            "code": "CMD-02",
            "value": "24"
          }
        ]
      },
      {
        "id": "2",
        "state": "Canada",
        "properties": [
          {
            "code": "CMD-04",
            "value": "50"
          },
          {
            "code": "CMD-05",
            "value": "60"
          }
        ]
      }
    ]
  }
}

I want to get the result as a new JSON, but without using pandas (and all those explode, flatten and normalize functions...). Is there any option to get this structure without using pandas or having an Out of memory issue?

The output should be:

{ "id": "1",
  "state": "USA",
   "code": "CMD-01",
   "value": "34"
},
{ "id": "1",
  "state": "USA",
   "code": "CMD-02",
    "value": "24",
},
{ "id": "2",
  "state": "Canada",
   "code": "CMD-04",
    "value": "50"
},
 { "id": "2",
  "state": "Canada",
   "code": "CMD-05",
    "value": "60"
},

CodePudding user response:

You can simply loop over the list associated with "geography" and build new dictionaries that you will add to a newly created list:

dict_in = {
  "data": {
    "geography": [
      {
        "id": "1",
        "state": "USA",
        "properties": [
          {
            "code": "CMD-01",
            "value": "34"
          },
          {
            "code": "CMD-02",
            "value": "24"
          }
        ]
      },
      {
        "id": "2",
        "state": "Canada",
        "properties": [
          {
            "code": "CMD-04",
            "value": "50"
          },
          {
            "code": "CMD-05",
            "value": "60"
          }
        ]
      }
    ]
  }
}
import json
rec_out = []
for obj in dict_in["data"]["geography"]:
    for prop in obj["properties"]:
        dict_out = {
            "id": obj["id"],
            "state": obj["state"]
        }
        dict_out.update(prop)
        rec_out.append(dict_out)

print(json.dumps(rec_out, indent=4))

Output:

[
    {
        "id": "1",
        "state": "USA",
        "code": "CMD-01",
        "value": "34"
    },
    {
        "id": "1",
        "state": "USA",
        "code": "CMD-02",
        "value": "24"
    },
    {
        "id": "2",
        "state": "Canada",
        "code": "CMD-04",
        "value": "50"
    },
    {
        "id": "2",
        "state": "Canada",
        "code": "CMD-05",
        "value": "60"
    }
]
  • Related