cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
41975
Views
0
Helpful
5
Replies

Python variable in json payload

damgol
Community Member

Hello,

I am not JSON expert so I am looking for help.

I want to pass on a python variable to payload in JSON format.

I was trying with code below but it doesn't work. I have info that JSON submitted is wrong.

My code is:

------------------------------------------------------------------------------------------------------------------------------------

import requests

url = "https://api.meraki.com/api/v1/organizations/XXX/configTemplates"

myname = "TESTNAME"

payload = '''{
"name": myname,
"timeZone": "America/Los_Angeles"
}'''

headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"X-Cisco-Meraki-API-Key": ""
}

response = requests.request('POST', url, headers=headers, data = payload)

print(response.text.encode('utf8'))

------------------------------------------------------------------------------------------------------------------------------------

Of course payload below works perfectly but I need variables to be able to pass them through argv in the future.

payload = '''{
"name": "TESTNAME",
"timeZone": "America/Los_Angeles"
}'''

1 Accepted Solution

Accepted Solutions

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

Try changing the request to (and keep the prior payload format I supplied):

response = requests.request('POST', url, headers=headers, data = json.dumps(payload))

View solution in original post

5 Replies 5

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

Try:

payload = {
"name": myname,
"timeZone": "America/Los_Angeles"
}

payload = {
"name": myname,
"timeZone": "America/Los_Angeles"
}

It doesn't work:

{"status":400,"error":"Bad request: There was a problem in the JSON you submitted"}

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

Try changing the request to (and keep the prior payload format I supplied):

response = requests.request('POST', url, headers=headers, data = json.dumps(payload))

Philip D'Ath
Meraki Community All-Star
Meraki Community All-Star

You'll also need to add at the top:

import json

Yes. It works.

Thank you very much!