locq
locq

Reputation: 301

Using python f-string in a json list of data

I need to push data to a website through their API and I'm trying to find a way to use F-string to pass the variable in the data list but couldn't find a way.

Here's what I've tried so far:

today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)

#trying to pass *tomorrow* value with f-string below

data = f'[{"date": "{tomorrow}", "price": {"amount": 15100}}]'

response = requests.put('https://api.platform.io/calendar/28528204', headers=headers, data=data)

How can I achieve this?

Upvotes: 7

Views: 20794

Answers (1)

BrownieInMotion
BrownieInMotion

Reputation: 1182

I would personally do something like this:

import json
import datetime

today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)

data = [{
    "date": str(tomorrow),
    "price": {
        "amount": 15100
    }
}]

print(json.dumps(data))

Of course, after this, you can do anything you want with json.dumps(data): in your case, send it in a request.

Upvotes: 12

Related Questions