Im having some issue trying to get ackknowalgemtns working with python.
thus far my code posts the message and gets the JSON sting in return but now i need a way to get the receipt out of the string
import requests
app_token = 'app token'
user_token = 'user token'
title = 'TEST MESSGAE'
text = 'text for polling'
params = {
'token': app_token,
'user': user_token,
'title': title,
'message': text,
'retry': 30,
'expire': 180,
'priority': 2,
'sound': 'siren',
}
msg = requests.post('https://api.pushover.net/1/messages.json', data=params)
print "POSTed message"
print msg.json()
output is
POSTed message
{u'status': 1, u'receipt': u'riD7NEukZsiDQpjhiNppphYnpue8uK', u'request': u'ef5f42d568e966cbf3d2b28c6579397c'}
The parameter is a case-sensitive, 30 character string containing the character set [A-Za-z0-9].
would this be a REGEX job ?
Because it's json type, so you can just use json module to load the data as a dict. For example if you have some json
data like this:
'{"message":"Hello there, wayfaring stranger"}'
Then you can use json.loads() function to load it as a dict like this:
>>> a = '{"message":"Hello there, wayfaring stranger"}'
>>> import json
>>> d = json.loads(a)
>>> d.items()
dict_items([('message', 'Hello there, wayfaring stranger')])
>>>
As you can see, a is string, but d is a dict. And now, 'Hello there, wayfaring stranger' is the value of key 'message'.
But msg.json() can auto covert the json data to dict, so you don't need json module here. So if you want to get 'ef5f42d568e966cbf3d2b28c6579397c', you can simply do something like this:
json_data = msg.json()
print json_data['request']
Related
For example
I have tried
import json
data = [{"ModelCode":"VH017","MakeCode":"VM020","VehicleTypeCode":"VC00000052","Year":2017,"IsActive":true,"RegistrationNumber":"KCC 254 ZY","IsApproved":true,"ApprovedBy":null,"Color":"BLUE","Id":"8c5062da-727b-40d5-b763-408cafdc53d8","_id":"3ce92939-4df7-4b9e-af48-647e218736da"},{"ModelCode":"VH024","MakeCode":"VM026","VehicleTypeCode":"VC00000053","Year":2008,"IsActive":false,"RegistrationNumber":"kkk 333k","IsApproved":false,"ApprovedBy":null,"Color":"blue","Id":"8c5062da-727b-40d5-b763-408cafdc53d8"}]
data_from_api = data.strip('][').split(',')
json.loads(data_from_api)
print(data_from_api)
I get a "NameError: name 'true' is not defined"
json.loads(str) is used to import data from a json string. If you want to create a json string from python data to save in a file as your header suggest then use json.dumps instead.
And as comments suggest. In python null is spelled None, false is False and true is True
import json
data = [
{
"ModelCode":"VH017",
"IsActive":True,
"ApprovedBy":None
},{
"ModelCode":"VH024",
"IsActive":False,
"ApprovedBy":None
}
]
json_str = json.dumps(data)
print(json_str)
I get lat and long from RESTful API. I need to craete new json field from another json fields.
I coded as below:
def collect_data():
data = requests.get(url = URL).json()
del data['positions'][1]
my_dict={}
my_dict["satlatitude"]= data["positions"][0]["satlatitude"]
my_dict["satlongitude"]= data["positions"][0]["satlongitude"]
new_data = json.dumps(my_dict)
new_data2 = {'geo' : {"lat": new_data["satlatitude"][0], "lon": new_data["satlatitude"][0]}}
es.index(index='satellitepositions', doc_type='satelitepos', body=new_data2)
schedule.every(10).seconds.do(collect_data)
while True:
schedule.run_pending()
time.sleep(1)
I don't get what I expect. What I need is:
new_data2 = {'geo' : {"lat":satlatitude , "lon": satlongitude}}
This satlatitude and satlongitude should come from data. I'm confusing how to extract the fields
from json documet.
Can someone help me to solve this?
RESTful data sample:
Json documet: {'info': {'satname': 'SPACE STATION', 'satid': 25544,
'transactionscount': 0}, 'positions': [{'satlatitude': 28.89539607,
'satlongitude': 90.44547739, 'sataltitude': 420.36, 'azimuth': 12.46,
'elevation': -52.81, 'ra': 215.55022984, 'dec': -5.00234017, 'timestamp':
1591196844, 'eclipsed': True}]}
new_data2 = {'geo' : {"lat":28.89539607 , "lon": 90.44547739,}}
But these values should change by each time.
I think the issue stems from this line :
new_data = json.dumps(my_dict)
When you do this, you dump your dict to a string. You can no longer index it by dict keys, it's a string.
But you still try later to index it by key :
new_data["satlatitude"][0]
This will crash. new_data is a string.
Do not juse json.dumps. I believe you don't need to serialize your data back to JSON, the elasticsearch library does this for you.
Solution :
Here's solution that (should be) working :
data = requests.get(URL).json()
new_data = {'geo':{'lat':data['positions'][0]['satlatitude'],
'lon':data['positions'][0]['satlongitude']}}
es.index(index='satellitepositions', doc_type='satelitepos', body=new_data)
Not that this has no validation that the requests has correctly returned, or that the data within conforms to your expectations. These should be checked if you want your code to be robust.
I need to specify taggings using put_object as follows:
s3client = boto3.client('s3')
response = s3client.put_object(Bucket = dest_bucket_name, Body = body, Key = dest_key, Tagging = tagging, ACL = acl )
My question is what data structure do I use to for the tagging variable?
I've seen various examples, but I can't get them to work. For example:
tagging = {'TagSet' : [
{
'Key': 'voiceid',
'Value': polly_voice
},
{
'Key': 'author',
'Value': book_author
},
. . .
]
}
gives me
"errorMessage": "Parameter validation failed:\nInvalid type for parameter Tagging, value: {'TagSet': [{'Key': 'voiceid', 'Value': 'Matthew'}, {'Key': 'author', 'Value': 'some guy'},...]}, type: <class 'dict'>, valid types: <class 'str'>"
I see that it's expecting a string, but then how do I specify the key:value pairs? And all the examples I've found use some specific data structure like in my example.
I doubt that this makes any difference, but I'm doing this in a lambda function.
To get this working, try the following code:
tags = 'voiceid={}&author={}'.format(polly_voice, book_author)
s3client = boto3.client('s3')
response = s3client.put_object(
Bucket=dest_bucket_name,
Body=body,
Key=dest_key,
Tagging=tags
)
You can refer this stackoverflow answer too.
URL encode your dictionary and pass the resulting string with this answer
For Python3:
>>> import urllib.parse
>>> tags = {'key1':'value1','key2':'value2'}
>>> tagging = urllib.parse.urlencode(tags)
>>> print(tagging)
key1=value1&key2=value2
I have a JSON file that looks like this:
[
"{'_filled': False,\n 'affiliation': u'Postdoctoral Scholar, University of California, Berkeley',\n 'citedby': 113,\n 'email': u'#berkeley.edu',\n 'id': u'4bahYMkAAAAJ',\n 'interests': [u'3D Shape',\n u'Shape from Texture',\n u'Shape from Shading',\n u'Naive Physics',\n u'Haptics'],\n 'name': u'Steven A. Cholewiak',\n 'url_citations': u'/citations?user=4bahYMkAAAAJ&hl=en',\n 'url_picture': u'/citations?view_op=view_photo&user=4bahYMkAAAAJ&citpid=1'}",
"\n"]
I am using python to extract the value of citedby. However, I am not able to figure.
Here is my code:
import json
json_data = open("output.json")
data = json.load(json_data)
print data[]
Now I know data would take an integer value whereas I would want to have it as a dictionary where in I could search using the key.
Is there any way I can achieve this?
import json
import ast
json_data = open("output.json")
data = json.load(json_data)
print ast.literal_eval(data[0])['citedby']
Why does this code give a KeyError?
output_format = """
{
"File": "{filename}",
"Success": {success},
"ErrorMessage": "{error_msg}",
"LogIdentifier": "{log_identifier}"
}
"""
print output_format.format(filename='My_file_name',
success=True,
error_msg='',
log_identifier='123')
Error message:
KeyError: ' "File"'
You need to double the outer braces; otherwise Python thinks { "File".. is a reference too:
output_format = '{{ "File": "{filename}", "Success": {success}, "ErrorMessage": "{error_msg}", "LogIdentifier": "{log_identifier}" }}'
Result:
>>> print output_format.format(filename='My_file_name',
... success=True,
... error_msg='',
... log_identifier='123')
{ "File": "My_file_name", "Success": True, "ErrorMessage": "", "LogIdentifier": "123" }
If, indicentally, you are producing JSON output, you'd be better off using the json module:
>>> import json
>>> print json.dumps({'File': 'My_file_name',
... 'Success': True,
... 'ErrorMessage': '',
... 'LogIdentifier': '123'})
{"LogIdentifier": "123", "ErrorMessage": "", "Success": true, "File": "My_file_name"}
Note the lowercase true in the output, as required by the JSON standard.
As mentioned by Tudor in a comment to another answer, the Template class was the solution that worked best for me. I'm dealing with nested dictionaries or list of dictionaries and handling those were not as straightforward.
Using Template though the solution is quite simple.
I start with a dictionary that is converted into a string. I then replace all instances of { with ${ which is the Template identifier to substitute a placeholder.
The key point of getting this to work is using the Template method safe_substitute. It will replace all valid placeholders like ${user_id} but ignore any invalid ones that are part of the dictionary structure, like ${'name': 'John', ....
After the substitution is done I remove any leftovers $ and convert the string back to a dictionary.
In the code bellow, resolve_placeholders returns a dictionary where each key matches a placeholder in the payload string and the value is substituted by the Template class.
from string import Template
.
.
.
payload = json.dumps(payload)
payload = payload.replace('{', '${')
replace_values = self.resolve_placeholders(payload)
if replace_values:
string_template = Template(payload)
payload = string_template.safe_substitute(replace_values)
payload = payload.replace('${', '{')
payload = json.loads(payload)
To extend on Martijn Pieters answer and comment:
According to MArtijn' comment, escaping the {..} pairs that are not placeholders is they way to go with nested dictionaries. I haven't succeded in doing that, so I suggest the following method.
For nested dictionaries I tried doubling up on any { and } of the nested dictionaries.
a='{{"names":{{"a":"{name}"}}}}'
a.format(name=123) output:
output: '{"names":{"a":"123"}}'
But this makes using format to change values inside a json string, a over-complex method, so I use a twist on the format command.
I replace ${param_name} in a json string. For example:
My predefined JSON looks like this:
my_json_dict = {
'parameter': [
{
'name': 'product',
'value': '${product}'
},
{
'name': 'suites',
'value': '${suites}'
},
{
'name': 'markers',
'value': '${markers}'
}
]
}
I provide this dictionary as values to replace instead of the parameters
parameters = {
'product': 'spam',
'suites': 'ham',
'markers': 'eggs'
}
And use this code to do the replacment
json_str = json.dumps(my_json_dict)
for parameter_name, parameter_value in parameters.iteritems():
parameter_name = '${'+parameter_name+'}'
json_str = json_str.replace(parameter_name, parameter_value)
json_dict = json.loads(json_str)