i have this json response
2022-02-08 14:01:56,048 sdc-web-sockets-pp /moe.com/SMART/logs/srf/srf-web-sockets.log: 2022-02-08 14:01:50,308 srf-web-sockets-pp-1 INFO [null:-1] (executor-thread-19) Op16 - Station : id-00003:vdeURRi8v8evU8AZWEnIwtbrucvjdeZyJL42, MessageId: a8a3e920-88e7-11ec-8f9b-c904565fadef, request message: {"action":"Boot","messageId":"a8d3r920-88e7-11ec-8f9b-c9074d5fadef","type":"CALL","uniqueId":"a8a3e920-88e7-11ec-8f9b-c9074d5fadef","payload":"{"Vendor":"vendorname","PointModel":"pointname","PointSerialNumber":"123456789","firmwareVersion":"1.1.1.8"}"}
I need to get the specific vars.
ex:
vendname=vendorname
model= pointname
modelsn= 123456789
firmversion=1.1.1.8
Can someone help me get these values from the request?
this is what i have so far
def getvalboot(e):
jsrqs = e.widget.get("1.0", "end-1c")
if "BOOT" in jsrqs.upper():
j1,j2ff = jsrqs.split(r"payload")
j2f = j2ff.replace("\\", "")
j2a = j2f[3:]
size = len(j2a)
j2 = j2a[:size - 2]
stud_obj = json.loads(j2)
print("1: ",stud_obj['Vendor'])
Ok, this works but im pretty sure there is a better way to do this, any idea?
CodePudding user response:
You could use the json libary wich comes defualt in built inn with python. The json libary can be used to convert the json file to a dictionary.
import json
f = open(r"file path", "r")
jsonf = f.read()
dict = json.loads(jsonf)
f.close()
CodePudding user response:
It is harder to help when you don't share a bit of code for context.
Assuming we have a context like this:
message = some_api_call()
I would try this:
data = message['payload'] #message is a dictionary and we'd like to access the payload/data
vendname = data['Vendor']
model = data['PointModel']
modelsn = data['PointSerialNumber']
firmversion = data['firmwareVersion']
I don't like the idea of creating a variable for each info, but since it is a small bit of code and we only have one row of data returned, it might not be relevant to discuss that.