0

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.

2
  • Your example string is neither JSON nor Python. In future, please provide correct examples. Commented Jul 13, 2016 at 16:26
  • thanks for the help Rob. it is a python list of json objects from my understanding. Commented Jul 13, 2016 at 17:07

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

Using this part of Rob's answer for the setup:

from json import loads, dumps
from collectins 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)

You could create a dictionary of territories, and then update your object:

territories = defaultdict(list)

for i, item in enumerate(python_object):
    territories[item['territory']].append(i)

for item in python_object:
    item['parameter_d'] = territories[item['territory']]

print(dumps(python_object, indent=2))

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.