Home > Software engineering >  create dataframe or list with dynamic json records
create dataframe or list with dynamic json records

Time:10-04

I am trying to create a dataframe or list that contains within each record a JSON schema that I will later insert through Rest-API. I have a sample that I'm trying to run but it doesn't work, I leave the code:

data_collect = df_res.collect()

for row in data_collect:
  v_id_person = '"'   row["Id"]   '"' 
  i= 1
  data[i] = """{
    "resourceType" : "Example",
    "identifier" : [{ 
            "use" : "official", 
             {
                    "system": "https://example/identif",
                    "code": "CC"
                }
            ] }, 
            "value" : """      str(v_id_person)     """
    } """
  i = i   1

display(data)

As I mentioned, the previous code does not achieve the objective of creating a list with the JSON structure for each record of the "FOR" cycle

What I want to achieve is:

display(data)
row[1] = {"resourceType" : "Example", "identifier" : [{ ... "value": 5412"
row[2] = {"resourceType" : "Example", "identifier" : [{ ... "value": 432"
row[3] = {"resourceType" : "Example", "identifier" : [{ ... "value": 1112"
...
row[n] = {"resourceType" : "Example", "identifier" : [{ ... "value": v_id_person

" What do you recommend me to do?

From already thank you for your time.

Regards

CodePudding user response:

Can you try with this

data = []
for row in data_collect:
  data.append(f"""{{
    "resourceType" : "Example",
    "identifier" : [{{ 
            "use" : "official", 
             {{
                    "system": "https://example/identif",
                    "code": "CC"
                }}
            ] }}, 
            "value" : "{row['Id']}"
    }} """)

CodePudding user response:

You can create DataFrame from json using

import pandas as pd
df = pd.read_json("data.json")
print(df)
  • Related