I am receiving data from a website via websocket however sometimes the websocket turns out to be empty outputting in {}. How can I give out a filter so that I only get the outputs of the data that is not empty. The if data != '{}' function does not work what could i do so that the function is valid?
import websocket
import pprint
import json
while True:
data= ws.fetch(dataset)
if data != '{}':
pprint.pprint(data)
check the length of the data
if len(data)>0
len(someDict) outputs number of items in the dictionary
First you have to change the "While" to "while".
Try this:
if data:
pprint.pprint(data)
Related
When we try to publish the json to Azure Event hub, EventData converting the JSON string. Instead of JSON string I need the JSON only at the consumer end.
response = {"status":"Active", "code":400}
event_batch = await producer.create_batch()
event_batch.add(EventData(json.dumps(response)))
At the consumer end I am getting the event as { body: '{"status":"Active", "code":400}', 'sequence_numbe':1} like this. how can we get only Json at consumer end like this
{ body: {"status":"Active", "code":400}, 'sequence_numbe':1}
Can anyone help?
You could use body_as_json method to achieve your requirement.
Sample Implementation :
jsonbody = event.body_as_json(encoding='UTF-8')
This will directly return us the JSON Object
Alternate Approach :
Get it as string and then convert the same to json object.
jsonbody = json.loads(event.body_as_str(encoding='UTF-8')))
My problem is the following: I've a list of Ips that I sorted in an nparray (ip_array), then I wanna do a multiple request with all of them and saving the outputs in a single json. (the APIKEY is really the api key in the code xD)
url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
for i in np.arange(1,4):
r[i] = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]}) #i tested the single request and it works in this way.
But then, i got
TypeError: 'Response' object does not support item assignment
And then, i tried replacing the last line with
r = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})
But, when i do
r.json()
I only get the last request (that is obvious).
Store response on every iteration:
url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
responses = []
for i in np.arange(1,4):
response = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})
responses.append(response.json())
responses list will contain all response objects.
I'm trying to pass a variable to the data field in requests.post() and I continue to get the error,
Error Response: {'error': {'message': 'Exception while reading request',
'detail': 'Cannod decode: java.io.StringReader#1659711'}, 'status': 'failure'}
Here is my code
#Fill array from CSV
temp=[]
for row in csv.iterrows():
index, data = row
temp.append(data.tolist())
#Create new asset for all assets in CSV
for index, row in enumerate(temp):
make = temp[index][0]
serial = str(temp[index][1])
date = str(temp[index][2])
response = requests.post(url, auth=(user, pwd), headers=headers,
data='{"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}')
I originally tried feeding it directly from the CSV using
str(temp[index][1])
This did not work, so I tried assigning str(temp[index][1]) to the variable serial and then passing the variable like that but that also results in the same error.
A point in the right direction would be great, thanks!
Instead of sending the request payload body in string, pass it in json form.
requests.post accepts string in data variable and json in json variable. I faced a same issue while trying to make my first REST call to ServiceNow instance via Python. Hope this helps.
response = requests.post(url, auth=(user, pwd), headers=headers,
json={"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial})
Remove the single quotes from the following :
data='{"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}'
Use
data = {"asset_tag":"test1", "assigned_to":"test2",
"company":"test3", "serial_number":serial}
rather than passing data=data, take data as dict and pass it as json=data.
I have the following script:
import boto3
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue
queue = sqs.get_queue_by_name(QueueName='')
for message in queue.receive_messages():
print(message.body)
# Let the queue know that the message is processed
message.delete()
It returns the following as the message's body:
{"Records":[{"eventVersion":"2.0","eventSource":"aws:s3","awsRegion":"us-west-2","eventTime":"2017-03-03T11:06:25.329Z","eventName":"ObjectCreated:Copy","userIdentity":{"principalId":"AWS:<id>:<lambda_name>"},"requestParameters":{"sourceIPAddress":"54.186.104.49"},"responseElements":{"x-amz-request-id":"8577BEEB91F199BF","x-amz-id-2":"<>="},"s3":{"s3SchemaVersion":"1.0","configurationId":"PutFromSisterBucket","bucket":{"name":"<bucket_name>","ownerIdentity":{"principalId":"<>"},"arn":"arn:aws:s3:::<bucket_nmae>"},"object":{"key":"<object_key>","size":1990,"versionId":"anHi0ukirRiApp4jyoSTz2oVGOejR6tJ","sequencer":"0058B94E3141A83718"}}}]}
How do I get the value of the "key" inside the "object"?
Currently, the entire result is a string. Is there any way I can do without string indexing or regex match?
If the variable message is string you need to load it with json.loads, to parse to dict type in python, so you can use it like a json:
import json
for message in queue.receive_messages():
message_dict = json.loads(message)
record = message_dict["Records"][0]
content = record["s3"]["object"]["key"]
print(content)
I am using boto library to read messages from SQS queue. My messages have text like this:
{ Command:XXXXXXXXXXX Key:XXXXXXX Input:XXXXXX} . Boto sends with base64 encoded and also reads it, so that if I read the message body then the text is there.
But how can I read the message like
Command = input['Command']
Key = input_message['Key'].split(',')
so that I can use those values for further processing...
I am quite new to Python also
Ok, you seem to have the input in some kind of a format - is it anything standardised? If not, you would need to parse the contents of your message and get the individual keys.
What I have been doing before in my projects was using JSON to facilitate data exchange between platforms.
If you do not have a luxury to edit your incoming data, you would need to do something like this (very naiive example):
input = "{ Command:XXXXXXXXXXX Key:XXXXXXX Input:XXXXXX }"
data = filter(lambda x: ":" in x, input.split())
message_dict = dict()
for item in data:
key, val = item.split(":")
message_dict[key] = val
Consider using good old fashioned JSON to easily send and receive dictionaries acrost the wire.
This test function verifies that the data format is very clear with JSON:
test_sqs.py
import json
import boto3
from moto import mock_sqs
#mock_sqs
def test_sqs():
sqs = boto3.resource('sqs', 'us-east-1')
queue = sqs.create_queue(QueueName='votes')
queue.send_message(MessageBody=json.dumps(
{'Command': 'drink', 'Key': 'beer', 'Input': 'tasty'}))
messages = queue.receive_messages()
assert len(messages) == 1
assert messages[0].body == (
'{"Input": "tasty", "Command": "drink", "Key": "beer"}')