You can create a helper function to dynamically populate the values in a dict
object with the necessary structure:
from __future__ import annotations
def build_api_request(names: list[str],
first: str, last: str,
email: str,
mobile_no: str,
country_code: str | int):
return {
"list_name": ','.join(names),
"subscriptions": [
{"first_name": first,
"last_name": last,
"email": email,
"mobile": {"number": mobile_no,
"country_code": str(country_code)}}
]
}
req = build_api_request(["listname0001", "listname0002", "listname0003"],
"Subscriber's First Name",
"Subscriber's Last Name",
"[email protected]",
"2003004000",
1)
import json
print(json.dumps(req, indent=2))
Prints:
{
"list_name": "listname0001,listname0002,listname0003",
"subscriptions": [
{
"first_name": "Subscriber's First Name",
"last_name": "Subscriber's Last Name",
"email": "[email protected]",
"mobile": {
"number": "2003004000",
"country_code": "1"
}
}
]
}
2
solved Dynamically build JSON in Python [closed]