I am attempting to filter the information from a websocket request.
I can complete my request fine however the response comes back with more information than I actually require. I want to filter this information out and then use it in a variable.
For example if I just use the sample code from ByBit websocket api
import json
from websocket import create_connection
ws = create_connection("wss://stream-testnet.bybit.com/realtime")
ws.send('{"op": "subscribe", "args": ["instrument_info.100ms.BTCUSD"]}');
bybitresult = ws.recv()
print(bybitresult)
ws.close()
I get the response below
{"topic":"instrument_info.100ms.BTCUSD","type":"snapshot","data":{"id":1,"symbol":"BTCUSD","last_price_e4":192785000,"last_price":"19278.50","bid1_price_e4":192780000,"bid1_price":"19278.00","ask1_price_e4":192785000,"ask1_price":"19278.50","last_tick_direction":"ZeroPlusTick","prev_price_24h_e4":192650000,"prev_price_24h":"19265.00","price_24h_pcnt_e6":700,"high_price_24h_e4":204470000,"high_price_24h":"20447.00","low_price_24h_e4":187415000,"low_price_24h":"18741.50","prev_price_1h_e4":192785000,"prev_price_1h":"19278.50","price_1h_pcnt_e6":0,"mark_price_e4":192886700,"mark_price":"19288.67","index_price_e4":193439800,"index_price":"19343.98","open_interest":467889481,"open_value_e8":0,"total_turnover_e8":1786988413378107,"turnover_24h_e8":65984748882,"total_volume":478565052570,"volume_24h":12839296,"funding_rate_e6":-677,"predicted_funding_rate_e6":-677,"cross_seq":5562806725,"created_at":"2018-12-29T03:04:13Z","updated_at":"2022-10-25T06:09:48Z","next_funding_time":"2022-10-25T08:00:00Z","countdown_hour":2,"funding_rate_interval":8,"settle_time_e9":0,"delisting_status":"0"},"cross_seq":5562806725,"timestamp_e6":1666678189180180}
However, I only want to use some of the data within the "data" string for example 'last_price' and 'timestamp_e6'. I have attempted this by trying to split the output string but am not having any luck at the moment.
Any help would be greatly appreciated. Thank you
The string received from ws.recv() is in JSON format. This string can be turned into a dictionary by doing something like:
import json
bybitresult = json.loads(ws.recv())
From there, you can get any data out of it as you would with a dictionary.
JSON in Python
Convert string into dictionary, which can be achieved by using json package, so that we can get values by referring keys👇.
import json
dict_Bybitresult = json.loads(Bybitresult)
last_price = dict_Bybitresult['data']['last_price']
timestamp_e6 = dict_Bybitresult['timestamp_e6']
Related
I am making my first API; any advice to improve my process is much appreciated.
I plan on passing JSON-like strings into the HTML request to this FastAPI microservice down there
#app.get("/create/{value}")
def createJSON(value:str):
person_json = value.strip()
fileName = person_json['Value']['0'] + person_json['Value']['1']
with open('%s.JSON','w') as writeFile:
writeFile.write(string)
return "Person has been created"
My HTTP request would look like this:
http://127.0.0.1:8000/create/{"Key":{"0":"name","1":"grad_year","2":"major","3":"quarter","4":"pronoun","5":"hobbies","6":"fun_fact","7":"food","8":"clubs","9":"res"},"Value":{"0":"adfasdfa","1":"adf'asd","2":"asd","3":"fads","4":"fa","5":"sdfa","6":"df","7":"asd","8":"fa","9":"df"}}
However, when doing this. The values passed are strings. Thus rendering the fileName portion of the code useless. How can I convert it to a Python dict? I have tried to use .strip(), but it did not help.
You're on the wrong track, Such a request should be essentially modeled as POST or a PUT request. That would allow you to send JSON in the body of the request and obtain it as a dict in python. You can see here
And even if you want to pass data in a GET request, there are query params
Coming back to the original doubt, you would have to use json.loads() to parse the json data and load it in a python dict then you can dump whatever file you like after that.
I'd recommend using the requests library
import requests
url = 'http://127.0.0.1:8000/create/'
params = dict(
name = 'Josh',
grad_year = '1987',
major = 'computer science',
quarter = '3'
)
resp = requests.get(url=url, params=params)
data = resp.json()
Then see here how to handle the JSON Response Content:
https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
The dict in the code I posted is different than the JSON you're trying to send through though. I assume you have a specific reason for having a "Key" array with the names than a "Value" array for the values of those specific names. But if not I'd recommend using a dictionary instead that way you can do things like:
fileName = person_json['name'] + person_json['grad-year']
Im trying to see if I'm able to get the response data as I'm trying to learn how to use regex on Locust. I'm trying to reproduce my test script from JMeter using Locust.
This is the part of the code that I'm having problem with.
import time,csv,json
from locust import HttpUser, task,between,tag
class ResponseGet(HttpUser):
response_data= ""
wait_time= between (1,1.5)
host= "https://portal.com"
username= "NA"
password= "NA"
#task
def portal(self):
print("Portal Task")
response = self.client.post('/login', json={'username':'user','password':'123'})
print(response)
self.response_data = json.loads(response.text)
print(response_data)
I've tried this suggestion and I somehow can't make it work.
My idea is get response data > use regex to extract string > pass the string for the next task to use
For example:
Get login response data > use regex to extract token > use the token for the next task.
Is there any better way to do this?
The way you're doing it should work, but Locust's HttpUser's client is based on Requests so if you want to access the response data as a JSON you should be able to do that with just self.response_data = response.json(). But that will only work if the response body is valid JSON. Your code will also fail if the response body is not JSON.
If your problem is in parsing the response text as JSON, it's likely that the response just isn't JSON, possibly because you're getting an error or something. You could print the response body before your attempt to load it as JSON. But your current print(response) won't do that because it will just be printing the Response object returned by Requests. You'd need to print(response.text()) instead.
As far as whether a regex would be the right solution for getting at the token returned in the response, that will depend on how exactly the response is formatted.
I am needing to automate a daily pull of app Annie data reviews and land them in S3. With the below I am trying to see if I can just pull one days worth of data but am getting an error 'TypeError: expected string or buffer'. I am new to python, can someone explain what I am doing wrong or another way to accomplish what I am trying to do?
import json
import requests
url = 'https://api.appannie.com/v1.2/apps/ios/app/331177714/reviews?
start_date=2016-1-01&end_date=2016-6-26&countries=US'
key = 'Authorization: bearer 585e46.....'
response = requests.get(url,
headers = {'Authorization':'bearer 585e46.....'})
data = json.loads(response.json())
.json method you're using comes from requests object and it already converts string to proper json. So you can do two things
Convert to json with requests object method:
data = response.json()
Get text from your response and turn into json with Python json lib:
data = json.loads(response.text)
I need a json file on the server to store some data, but it won't be too big to need a database. So I try to read the file, and after I finish using it I will need to overwrite the data to keep update.
I tried like this:
#app.route("/json")
def readwrite():
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
json_url = os.path.join(SITE_ROOT,'static', 'test.json')
token = open(json_url)
return token
But I get a 404 error on those. I'm not sure how I can read out those data and further rewrite. Please help if you see any problem in my code. Thanks!
You are returning the file handle via HTTP to the client. Get the json data and send that.
stored_json = token.readlines()
token.close()
return stored_json
I know nothing about Python but would like to clone this script with jquery using ajax post.
To do that i need to know what this script is doing in the first place.
import requests
import json
params = {'nearest': True, 'imageurl': img, 'timestamp':140000}
request = requests.post('http://example.com/api/upload/', data=params)
output = request.json()
print json.dumps(output['files'][0]['predicted_classes'])
Thanks. If something is unclear please comment and i'll clarify.
import requests
import json
above line imports two modules Request(contain methods for sending request to the server) and json(to serialise/deserialise data to json)
params = {'nearest': True, 'imageurl': img, 'timestamp':140000}
creating a dictionary with key value .here it is used to pass parameter
response= requests.post('http://example.com/api/upload/', data=params)
this is used to send Post resquest . here post is method in request module with parameters(Url,data_to_send)
output = response.json()
output has the response in json format
print json.dumps(output['files'][0]['predicted_classes'])
json dumps is used to convert to json format
This code does the following:
1) #First it imports the external modules.
2) #Next it defines params as a dictionary with 3 entries.
3) #Then it uses request libraries to get the file and transfigures "params" into a json object
4) #Lastly, the code prints the request.
To see the request you may need to use an image library to see what you gathered from the World Wide Web.