1

I have a script that sends a JSON UTF-8 encoded Byte string to a socket. (A github project: https://github.com/alios/raildriver). Now I'm writing the python script that needs to read the incoming data. Right now I can receive the data and print it to the terminal. With the following script: https://www.binarytides.com/code-telnet-client-sockets-python/

Output:

data = '{"Current": 117.42609405517578, "Accelerometer": -5.394751071929932, "SpeedometerKPH": 67.12493133544922, "Ammeter": 117.3575210571289, "Amp": 117.35590362548828, "Acceleration": -0.03285316377878189, "TractiveEffort": -5.394751071929932, "Effort": 48.72163772583008, "RawTargetDistance": 3993.927734375, "TargetDistanceBar": 0.9777777791023254, "TargetDistanceDigits100": -1.0, "TargetDistanceDigits1000": -1.0}'

The problem is that I can't find how to read the JSON array. For example read "Ammeter" and return its value 117.357521057289 to a new variable.

All the data is being received in the variable data

The code I have right now:

decodedjson = data.decode('utf-8')
dumpedjson = json.dumps(decodedjson)
loadedjson = json.loads(dumpedjson)

Can you please help me?

0

1 Answer 1

4

You are encoding to JSON then decoding again. SImply not encode, remove the second line:

decodedjson = data.decode('utf-8')
loadedjson = json.loads(decodedjson)

If you are using Python 3.6 or newer, you don't actually have to decode from UTF-8, as the json.loads() function knows how to deal with UTF-encoded JSON data directly. The same applies to Python 2:

loadedjson = json.loads(data)

Demo using Python 3.7:

>>> data = b'{"Current": 117.42609405517578, "Accelerometer": -5.394751071929932, "SpeedometerKPH": 67.12493133544922, "Ammeter": 117.3575210571289, "Amp": 117.35590362548828, "Acceleration": -0.03285316377878189, "TractiveEffort": -5.394751071929932, "Effort": 48.72163772583008, "RawTargetDistance": 3993.927734375, "TargetDistanceBar": 0.9777777791023254, "TargetDistanceDigits100": -1.0, "TargetDistanceDigits1000": -1.0}'
>>> loadedjson = json.loads(data)
>>> loadedjson['Ammeter']
117.3575210571289
Sign up to request clarification or add additional context in comments.

10 Comments

What error? Where? Did you verify that you have Python 3.6 or an older version?
decodedjson = data.decode('utf-8') loadedjson = json.loads(decodedjson) print loadedjson['Ammeter']There is an error 'Traceback (most recent call last): loadedjson = json.loads(decodedjson) File "/usr/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded'
You are using python 2 then. Without a proper sample we can’t help you then. We need more than a single print output
@RubinNederlof: put differently: we don't know how data is being loaded, if this is being executed in a loop, etc. The specific data value you have there differs from what you posted here. Perhaps there is extra data in it or this loop iteration produced an empty data value. Without more context on how to reproduce the error, we can't help more than what we've done so far.
The data is being send by a github project to read data from a simulation game: github.com/alios/raildriver. The data is being received by this script: binarytides.com/code-telnet-client-sockets-python. I hope this is the information you need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.