Alex.Widmore Alex.Widmore - 4 months ago 11
JSON Question

How to do a group by operation on a list of json objects in python?

I am working on a Python script where I need to group by a key in a list of JSON objects.

I’ve a list of a large number of JSON objects in python in the following format:

[{'name': xyz,
'territory': abc,
'parameter_a': 1,
'parameter_b': 2,
'parameter_c': 3},
…]

Now I want to create a tag (say parameter_d) which should say the number in the grouped by count the number of times a specific territory (Say ‘abc’) has occurred in the whole list of json objects.
E.g. Territory abc occurs 3 times in the list, so i want the parameter_d to hold values 1,2,3 for the different instances where the territory abc occurred.
Thanks in advance for the help.

Answer
from json import loads, dumps
from collections import defaultdict

json_string = """
[
    {"name": "xyz", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
    {"name": "qrs", "territory": "def", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
    {"name": "tuv", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
    {"name": "abc", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3}
]"""

# Step 1: convert from JSON to Python:
python_object = loads(json_string)

# Step 2: Add `parameter_d` to each item in list,
# using defaultdict(int) as a counter:
counts = defaultdict(int)
for item in python_object:
    counts[item["territory"]] += 1
    item["parameter_d"] = counts[item["territory"]]

# Step 3: convert from Python to JSON
json_string = dumps(python_object, indent=2)
print json_string
Comments