1

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?

2 Answers 2

1

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

2 Comments

Thanks, I think this will probably work. For my own education, do you mind also sharing what it would look like using nested loops instead of the comprehension? I'm new to Python and still struggle with them so this will help me learn.
@MSD Done.. :-)
1

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

2 Comments

Thanks, but I should have worded my question more clearly. I was mostly interested in combining the objects into a single data structure (eventually pandas), and not actually printing the sample output. My apologies for the confusion, but I appreciate the response nonetheless.
No worries @MSD.

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.