0

Similar to this question I want to convert a list of json to a pandas dataframe and convert this:

[
 {
   "origin": 101011001,
   "destinations": [
    {"destination": 101011001, "people": 7378},
    {"destination": 101011002, "people": 120}
   ]
 },
 {
   "origin": 101011002,
   "destinations": [
    {"destination": 101011001, "people": 754},
 }
]

To:

origin    destination  people
101011001 101011001    7378
101011001 101011002    120 
101011002 101011001    754

However, my json list is a variable and change through a loop because I used multithreading to get a list of json data. Regarding this, I must add """ at the beginning and end of the json list. But, here it is a variable not a string. How can I add """ to the json list? Is there any easier way to convert a json list into a pandas dataframe?

The main code is:

with ThreadPoolExecutor(max_workers=14) as executor:
    for url in cid_str:
        sleep(0.1)
        processes.append(executor.submit(download_file, url))
 for index, task in enumerate(as_completed(processes)):
        jdata = task.result()

jload = json.loads('"""' + jdata + '"""')
df = json_normalize(jload,"destinations",['origin'])
V.Nouri
  • 207
  • 3
  • 15

0 Answers0