Functional Python conversion from long to wide JSON dictionary list

I have a JSON object that I am trying to remake for analysis in different ways, and I am looking for a functional transformation to aggregate one field based on two fields with a unique key.

My dataset is as follows:

myjson = 

[
{
"name": "Fred",
"class": "Algebra",
"topic" : "polynomials",
"extra" : "True"
},
{
"name": "Fred",
"class": "Algebra",
"topic" : "polynomial division",
"extra" : "False"
},
{
"name": "Fred",
"class": "Algebra",
"topic" : "solving",
"extra" : "True"
},
{
"name": "Willbert",
"class": "Dance",
"topic" : "Fancy",
"extra" : "False"
},
{
"name": "Willbert",
"class": "Dance",
"topic" : "Country",
"extra" : "True"
}
]

I would like to use Name and Class as unique keys to aggregate the field to those where the contents of the "extra" field are different, I would like all of them to save data associated with the first input - that is, they do not need to be combined, but you need to take only value from one record.

So, I would like to include this:

[
{
"name": "Fred",
"class": "Algebra",
"topic" : ["polynomials","polynomial division","solving"],
"extra" : "True"
},
{
"name": "Willbert",
"class": "Dance",
"topic" : ["Fancy","Country"],
"extra" : "False"
}
]

Or even combine topics together as a string:

[
{
"name": "Fred",
"class": "Algebra",
"topic" : "polynomials polynomial division solving"
},
{
"name": "Willbert",
"class": "Dance",
"topic" : "Fancy Country"
}
]

groupby, , , , .

UPDATE

, , ...

groups = itertools.groupby(myjson,lambda x: (x['name']))
[(k,list(g)) for k,g in groups]

. "" "".

:

groups = itertools.groupby(myjson,lambda x: (x['name'],x['class']))
[(k,list(g)) for k,g in groups]

2

, , , 2 - , - itertools groupby?

0
1

:

data = {}
key = operator.itemgetter("name", "class")
for record in myjson:
    k = key(record)
    if k in data:
        data[k]["topic"].append(record["topic"])
    else:
        data[k] = record.copy()
        data[k]["topic"] = [record["topic"]]
result = data.values()

, , "topic". , , .

+1

All Articles