简体   繁体   中英

How to Combine JSON Objects From Different Arrays With Python

I am trying to loop through different JSON arrays using Python, in order to combine all the objects into a single data structure. The JSON looks like this:

data = {
  "Wednesday, Apr 3, 2019": [
    {
      "id": "000",
      "keyid": "4273666087",
      "name": "Raptor",
      "symbol": "RPT",
    },
    {
      "id": "111",
      "keyid": "1818114564",
      "name": "Duck",
      "symbol": "DUK",
    }
  ],
  "Tuesday, Apr 2, 2019": [
    {
      "id": "222",
      "keyid": "8032408148",
      "name": "Hawk",
      "symbol": "HWK",
    },
    {
      "id": "333",
      "keyid": "0362766431",
      "name": "Goose",
      "symbol": "GOO",
    }
  ]
}

Since it looks like a dictionary, I tried doing:

for item in data.values():
   print(item)
   print("\n")

which combines each array's objects into a separate lists. But I want all objects to be part of the same data structure, in order for the end result to look something like this:

id  | keyid      | name   | symbol
-----------------------------------
000 | 4273666087 | Raptor | RPT
-----------------------------------
111 | 1818114564 | Duck   | DUK
-----------------------------------
222 | 8032408148 | Hawk   | HWK
-----------------------------------
333 | 0362766431 | Goose  | GOO
-----------------------------------

What's the best way of doing this?

Use pandas :

import pandas as pd
df = pd.DataFrame([x for k,v in data.items() for x in v])
print(df)

Output:

    id       keyid    name symbol
0  000  4273666087  Raptor    RPT
1  111  1818114564    Duck    DUK
2  222  8032408148    Hawk    HWK
3  333  0362766431   Goose    GOO

You can do it in Regular Python but it will be a big hassle, so I support using pandas which can do it in one line.

As you want nested loops:

import pandas as pd
l = []
for k,v in data.items():
    for x in v:
        l.append(x)
df = pd.DataFrame(l)
print(df)

You're very close. You want to go one further, and print the values of each row.

print("id", "keyid\t", "name", "symbol", sep='\t|')

for group in data.values():
    for row in group:
        print("\t|".join([str(i) for i in row.values()]))

To produce:

id      |keyid          |name   |symbol
000     |4273666087     |Raptor |RPT
111     |1818114564     |Duck   |DUK
222     |8032408148     |Hawk   |HWK
333     |0362766431     |Goose  |GOO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM