Skip to content
Advertisement

How to group a json by a nested key using Python?

Lets say we have a json object in Python:

myJson = [
    {
        "id": "123",
        "name": "alex",
        "meta": {
                    "city": "boston"
        }
    },
    {
        "id": "234",
        "name": "mike",
        "meta": {
                    "city": "seattle"
        }
    },
    {
        "id": "345",
        "name": "jess",
        "meta": {
                    "city": "boston"
        }
    }
]

What is the most efficient way to group this data by city, so that we end up with a json in which we group the data by city such that we end up with a json as:

myNewJson = [
    {
     "city": "boston",
     "people": [ ... ... ]
    },
    {
     "city": "seattle",
     "people": [ ... ]
    }
]

… in which the content of the people are included in “people” key.

Thanks!

Advertisement

Answer

Try:

myJson = [
    {"id": "123", "name": "alex", "meta": {"city": "boston"}},
    {"id": "234", "name": "mike", "meta": {"city": "seattle"}},
    {"id": "345", "name": "jess", "meta": {"city": "boston"}},
]

out = {}
for d in myJson:
    out.setdefault(d["meta"]["city"], []).append(d["name"])

out = [{"city": k, "people": v} for k, v in out.items()]
print(out)

Prints:

[
    {"city": "boston", "people": ["alex", "jess"]},
    {"city": "seattle", "people": ["mike"]},
]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement