This is the payload for a request on python:
payload = "{\"depositCoin\":\"eth\",\"destinationCoin\":\"btc\"}"
I want to make the btc part variable, in other words, able to be changed depending on user input. I have tried replacing the string with a variable name but it doesn't work. I have also tried to replace it with a %d and then using % variable but it still does not work. Thanks for any help in advance!
Just use the json module to dump your dictionary into the proper format.
import json
payload = {"depositCoin": "eth", "destinationCoin": None}
coin = input()
print(coin)
# 'lite'
payload["destinationCoin"] = coin
json.dumps(payload)
'{"depositCoin": "eth", "destinationCoin": "lite"}'
Related
So, i wrote this code:
json_data = "{\"photoBytes\":" + str(bytearray(open("image.png", "rb").read())) + "}"
service.people().updateContactPhoto('people/c4942919248052589775', json_data)
but it gives me this error:
TypeError: method() takes 1 positional argument but 3 were given
I cannot understand why. As the docs say, I should use it like this...
You are passing a string value as a request body which should be in json format.
Try using json.dumps() first on your json_date before passing it as a parameter in updateContactPhoto().
Your code should look like this:
json_data = "{\"photoBytes\":" + str(bytearray(open("image.png", "rb").read())) + "}"
service.people().updateContactPhoto('people/c4942919248052589775', json.dumps(json_data))
I was able to figure this out with a lot of trial and error and I found that the photo needs to be base64 encoded and the best method to get that into JSON below (thanks to this answer for this tip).
Here's the working code for me below
import base64
JSON = """{
"photoBytes": "%s"
}
""" % (base64.b64encode(open("test.png", "rb").read()).decode('utf-8'))
update_body = json.loads(JSON)
service.people().updateContactPhoto(resourceName="people/123456789", body=update_body).execute()
I want to be able to read json API information and depending on the information make something happen. For example:
I get this information from a Streamelements API.
{
"donation":{
"user":{
"username":"StreamElements"
"geo":"null"
"email":"streamelements#streamelements.com"
}
"message":"This is a test"
"amount":100
"currency":"USD"
}
"provider":"paypal"
"status":"success"
"deleted":false
"_id":"5c0aab85de9a4c6756a14e0d"
"channel":"5b2e2007760aeb7729487dab"
"transactionId":"IMPORTED"
"createdAt":""2018-12-07T17:19:01.957Z""
"approved":"allowed"
"updatedAt":""2018-12-07T17:19:01.957Z""
}
I then want to check if the amount on that specific tip is $10 and if that is the case I want something to happen.
This is what I have so far but I do not know how to get the right variable:
data = json.loads(url.text)
if (data[0]['amount'] == 10):
DoTheThing();
The amount field is under the inner donation object:
data = json.loads(url.text)
donation_amount = data["donation"]["amount"]
if donation_amount == 10:
# do something
Verified using the Stream Elements Tips API documentation.
I am trying to add potential arguments to a variable dynamically, and send it to the filter function of exchangelib. I can't get it work.
This is what's currently working:
sender='abd#google.com'
accountSource.bulk_move(ids=sourceAccountFolder.filter(sender), to_folder=destinationAccountFolder)
This is what I would like to do:
params = { sender: 'asd#google.com', subject:'asdf'}
accountSource.bulk_move(ids=sourceAccountFolder.filter(params), to_folder=destinationAccountFolder)
This doesn't work
The idea is to dynamically add params if needed. For example, I could add subject = 'subjectSample' to params variable.
Thank you
Update:
This did work:
params = { 'sender': 'anemail#abc.com', 'subject__icontains': 'xxx'}
accountSource.bulk_move(ids=sourceAccountFolder.filter(**params).values('item_id', 'changekey'), to_folder=destinationAccountFolder)
I have this String that I need to pass into a REST request
{"notification":{"tag":"MyTag"}}
I'm trying to turn into an object using the JSON module in python.
This is my attempt so far
import json
obj = json.dumps([{'notification': ('{tag : MyTag}')}])
But it isn't parsed correctly so the REST request won't work. Anyone have any ideas?
Just dump your dictionary as is, replace:
obj = json.dumps([{'notification': ('{tag : MyTag}')}])
with:
obj = json.dumps({"notification": {"tag": "MyTag"}})
I want to make a HTTP wrapper for twitch.tv in python. How would I do this? I know how to use HTTP GET and that's about it. I would like it to work like this:
import twichtvwrapper
twich = twichtvwrapper(useragent, username, password).
channel = twich.channel(channelname)
then all the json properties would go in here like:
print(channel.game) #this would say the last played game
print(channel.displayname) #this would print the display name of the channel
print(cahnnel.link.chat) #this will return the chat link
etc.
How would I go about making this wrapper?
You can convert between Python dicts and JSON strings with the json module of the Standard Library.
import json
# package a python dict as json
dict0 = {'spam': 'data', 'eggs': 'more data'}
pack = json.dumps(dict0)
# turn it back to a dict
dict1 = json.loads(pack)
>>> print dict1['spam']
data
So, if your program gets a JSON response from a HTTP request, just convert it to a dict and use regular Python dict methods to do the rest.