0

What would be the code to automate the fetching of data and creation of multiple JSON payloads with same parameters and structure, only the values change every-time?

Eg: I have a payload like this:

{
    "advertiserProfileInputs": [
        {
            "advertiserIdType": "ADVERTISER",
            "advertiserId": "**XYZ**",
            "marketplaceId": "US001",
            "process": "sponsor"
        }
    ]
}

I have a dataset (in an excel file) of multiple advertiserType-(Advertiser, Entity etc) , advertiserId-(XYZ, ABC, DEF, GHI etc) , process-(sponsor, store etc). [Note : Each array has only one and unique set of data only.]

My target is to create the payload with the same structure but every-time the data changes automatically. The data may change for multiple parameters as well. Expected Output :-

{
    "advertiserProfileInputs": [
        {
            "advertiserIdType": "ADVERTISER",
            "advertiserId": "ABC",
            "marketplaceId": "US001",
            "process": "sponsor"
        }
    ]
}
{
    "advertiserProfileInputs": [
        {
            "advertiserIdType": "ADVERTISER",
            "advertiserId": "GHI",
            "marketplaceId": "US001",
            "process": "store"
        }
    ]
}
]

. . . and so on.

Tried looking through a multiple number of solutions but all of them were mostly related to different problems.

  • There are several modules available for handling Excel documents. Pick your favourite. The way you construct the Python dictionaries will depend on how the data are organised in the spreadsheet(s). What do you plan to do with the dictionaries once you've built them? – DarkKnight Nov 14 '22 at 06:12
  • please post output of `df.head()` of excel file for sample data. – Jaydeep Devda Nov 14 '22 at 06:15
  • @Cobra , I need to build JSON payloads with the data from excel. The plan would be -> Fetch the data from excel -> Create just the same payload structures with different sets of data pasted in them. Now with this o/p, I can simply copy and paste it in the APIs to get the o/p. If I get a solution to my ask, I wouldn't have to copy and paste different data in these payloads again and again, to run them in APIs. If I can just create such structures, I can avoid the changing of data manually every time. – Soham Sinha Nov 14 '22 at 08:56
  • @JaydeepDevda, I am unaware of it. If you are asking what the excel sheet looks like, it is just simple 3 columns (advertiserID, process and marketplace), under these columns are different data. – Soham Sinha Nov 14 '22 at 08:57
  • Please follow below steps and you will get what you want. 1. how to read excel files in python 2. how to iterate dataframe row by row and access cell value in python 3. how to create json in python. 4. how to save json data in text file in python .. – Jaydeep Devda Nov 15 '22 at 03:51
  • Thankyou @JaydeepDevda. I will try what you said and get back to you. – Soham Sinha Nov 15 '22 at 06:07
  • @JaydeepDevda , most part of the problem is solved. Just one thing is holding me back. In the advertiserID column the inputs are being taken as number whereas I want it to be taken in form of strings. So, that when the payload is created the IDs are enclosed within "xxxx" and not like xxxx. This is happening since for the dataset I entered in excel the IDs are all numbers but some IDs have characters as well. Thus, I want it to accept as string. Any idea how? – Soham Sinha Nov 15 '22 at 14:25
  • Please search how to convert int to string in python ? or refer https://stackoverflow.com/a/13384494 Thanks – Jaydeep Devda Nov 16 '22 at 03:49

0 Answers0