1

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

3
  • Can you share your json structure that you want to convert to dataframe ? Commented Oct 3, 2022 at 18:25
  • thanks for your answer. I want to add the json structure that appears posted, this for each record in the new dataframe "data", thanks Commented Oct 3, 2022 at 18:26
  • 1
    I am not able to get you. Can you please write what you have and what you want i.e. input, output and expected_output. Commented Oct 3, 2022 at 18:27

2 Answers 2

1

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

8 Comments

thanks you for response. I try to implement you comment, but return error "f-string: expressions nested too deeply". I try resolve this. If you have any other comment, I would appreciate it. Thank you
@Gonza My bad. I have edited the answer now check
Thank you very much for your answer, now I can run as I wanted. I have a query about it: is this that I just created a dataframe? why doesn't it work for me with the display() function but it does with print() ?? Thank you!!
For that you need to show the display function.
because I want to validate the data, since I will create a much larger json. How can I see the structured dataframe type diplsya()? Thanks!
|
0

You can create DataFrame from json using

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

1 Comment

thanks for you response. I've posted an edit to the expected output.

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.