Home > Blockchain >  Using .format() in a dictionary with an API call
Using .format() in a dictionary with an API call

Time:01-14

I am making an api call and in doing so I have to manually change the endDate in the payload every day. I can not use .format() inside a dictionary. Can anyone help out?

Current payload: where I am changing the endDate manually

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000 00:00\",\"***endDate\":\"2022-01-13***T00:00:00.0000000 00:00\",\"format\":\"csv\"}"

Expected payload:

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000 00:00\",\endDate\":\"{}T00:00:00.0000000 00:00\",\"format\":\"csv\"}".format(today)

Here today will be a variable with today’s date

CodePudding user response:

That's a string, not a dictionary and the .format thing that you want, works. Guessing that this is in fact JSON data, the normal way to do this sort of thing is to build a python dict and serialize it later. Using a python "f-string" makes it simple to call a function in the string format specification itself. datetime.datetime.utcnow() gives the current UTC time. It can be converted to a date and its isoformat method writes the format you want. So,

import datetime as dt
import json

data = {
    "dimensions": ["AdsetId", "Adset", "CampaignId", "Campaign",
        "Device", "Day", "Month", "Year", "Week", "Os"],
    "metrics": ["AdvertiserCost", "Displays", "ClickThroughRate", 
        "Cpc", "AppInstalls", "Clicks"],
    "timezone": "UTC", 
    "advertiserIds": "69957", 
    "currency": "USD", 
    "startDate": "2022-01-01T00:00:00.0000000 00:00", 
    "endDate": f"{dt.datetime.utcnow.date.isoformat()}T00:00:00.0000000 00:00", 
    "format": "csv"}
    
    
payload = json.dumps(data)
print(payload)

CodePudding user response:

if your payload is a string and u just want to be able to format it with a specific variable, u can use f-strings, for example:

today = datetime.strftime(datetime.now(), "%Y-%m-%d")

payload = f"{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000 00:00\",\endDate\":\"{today}T00:00:00.0000000 00:00\",\"format\":\"csv\"}"

CodePudding user response:

In my opinion, a very straightforward way to do this would be along the lines of what @Olvin Roght suggested in a comment, which was essentially this:

  1. Convert payload into a dictionary using json.loads().
  2. Modify the "endDate" in the dictionary.
  3. Convert the dictionary back into a string.

Which doesn't involve .format() at all.

from datetime import datetime, timezone
import json

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000 00:00\",\"endDate\":\"2022-01-13T00:00:00.0000000 00:00\",\"format\":\"csv\"}"
payload = json.loads(payload)
dt = datetime.now(timezone.utc).replace(minute=0, hour=0, second=0, microsecond=0)
payload["endDate"] = dt.isoformat()
print(json.dumps(payload, indent=4))

Output:

{
    "dimensions": [
        "AdsetId",
        "Adset",
        "CampaignId",
        "Campaign",
        "Device",
        "Day",
        "Month",
        "Year",
        "Week",
        "Os"
    ],
    "metrics": [
        "AdvertiserCost",
        "Displays",
        "ClickThroughRate",
        "Cpc",
        "AppInstalls",
        "Clicks"
    ],
    "timezone": "UTC",
    "advertiserIds": "69957",
    "currency": "USD",
    "startDate": "2022-01-01T00:00:00.0000000 00:00",
    "endDate": "2022-01-13T00:00:00 00:00",
    "format": "csv"
}
  •  Tags:  
  • Related