3

I have a dataframe that I need to pass as a nested json string into an email service provider api as a json string.

My dataframe looks like this:

email_address     first_name       last_name
[email protected]            adam             apple
[email protected]            bobby            banana

The contacts in the dataframe is what I need to pass into the email service provider API, and this needs be a nested JSON string like so:

{
    "import_data": [{
        "email_addresses": ["[email protected]"],
        "first_name": "Hector",
        "last_name": "Smith"
    }, {
        "email_addresses": ["[email protected]"],
        "first_name": "Jane",
        "last_name": "Doe"
    }, {
        "email_addresses": ["[email protected]"],
        "first_name": "Pradeep",
        "last_name": "Patel"
    }],
    "lists": ["1234567890"]
}

I am not sure how I would create nested json string via the 'to_json' command from pandas and at the same time insert the word "import_data" as above into the json string. I know I can hard code the a column with in the dataframe for "lists" and pass that in as well. List ID will always be static.

Here is code for my API response:

headers = {

    'Authorization': '',
    'X-Originating-Ip': '',
    'Content-Type': '',

    }

update_contact = '{"import_data": [{"email_addresses": ["[email protected]"],"first_name": "test","last_name": "test"},{"email_addresses": ["[email protected]"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["[email protected]"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1234567890"]}'

r = requests.post('url', headers=headers ,data = update_contact)

print(r.text)

3 Answers 3

3

I believe the API asked for application/json, if this is really the case, you should send it like this

headers = {}

update_contact = {"import_data": [{"email_addresses": ["[email protected]"],"first_name": "test","last_name": "test"},{"email_addresses": ["[email protected]"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["[email protected]"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1234567890"]}

r = requests.post('url', headers=headers ,json= update_contact)

print(r.text)
Sign up to request clarification or add additional context in comments.

5 Comments

let me test that out. and you are correct can use application/json
and because you didn't set any headers, you can just send it without the headers keyword argument @RustyShackleford
that works just fine, but how do I pass in many rows from my dataframe and insert the word 'import_data" only once?
@RustyShackleford you can try update_contact['import_data'].append(yourRow)
I have been playing around with this last few days. My issue is that I need to pass the phrase 'import_data' once and list ID once. When I hardcode list_id into the 'lists' column, every row gets the list_id. How do I only pass 'import_data' once and 'lists' once? I am not sure what you mean by (yourrow). I will have more than 2 people in the dataframe so the approach needs to be scalable to thousands of users.
2

Format the data using orient='records' of to_dict(), then just format your the dictionary to json

#converted emails to list, may not be necessary...
df.email_address = df.email_address.apply(lambda x: [x])

import json

update_contact = json.dumps({'import_data':df.to_dict(orient='records'),
                             'lists':["1234567890"]})

Comments

1

You can use json_variable.append(NewValues) so that it will append any of your data blocks into a json value. Refer to below skeleton and re-build according to your inputs.

enter code here


import json
Json_List=[]

for i in Excel_data:
    Newdata = {
    'Name':Read_name_From_Excel(i),
    'Age':Read_age_From_Excel(i),
    'Email':Read_Email_From_Excel(i),
    'Amount':Read_Amount_From_Excel(i)
    }

    Json_List.append(Newdata)

Comments

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.