How to convert JSON rows to columns

Question:

I have a JSON data structure that is row-based:

rows = [
    {'name': 'tim', 'age': 113}, 
    {'name': 'tess', 'age': 111},
]

that I would like to convert to column-based data:

columns = {
    'name': ['tim', 'tess'],
    'age': [113, 111],
}

What is the most readable way to do this if there are lots of rows and columns?

Asked By: jnnnnn

||

Answers:

Use collections.defaultdict:

from collections import defaultdict

rows = [
    {'name': 'tim', 'age': 113}, 
    {'name': 'tess', 'age': 111},
]

d = defaultdict(list)
for r in rows:
    for k, v in r.items():
        d[k].append(v)

print(d)
# defaultdict(<class 'list'>, {'name': ['tim', 'tess'], 'age': [113, 111]})
Answered By: Austin

You could try:

from collections import defaultdict
x = defaultdict(list)
for item in rows:
    for key, value in item.items():
        x[key].append(value)
Answered By: Binh
new_rows={'name':[],'age':[]}
for row in rows:
    new_rows['name'].append(row['name'])
    new_rows['age'].append(row['age'])

Here, I am creating a new dict i.e ‘new_rows’. Now iterating over the elements of ‘rows’, appending the values of each dict element to ‘new_rows’ keys

Answered By: Mehul Gupta

Here’s what I ended up with:

columns = {k: [d[k] for d in rows] for k in rows[0]}

Explanation:

Iterate over the keys of the first row (rows[0]), and wrap that in a dictionary
comprehension. The dictionary comprehension then goes and gets the values for
each key from each row.

So the whole thing is a bunch of list comprehensions (one for each key) that iterate over the rows, wrapped
in a dictionary comprehension that iterates over the keys of the first row.

Answered By: jnnnnn