-1

I am using this Python project to try and interact with my Lego SBrick.

If I use the following piece of code (once connected to the SBrick):

json_response = client.rr_get_adc(sbrick_id='11:22:33:44:55:66', timeout=5)

I can then print the json_response to give me:

{"ret_code": 100, "temperature": 42.53253535, "voltage": 8.3954548125663}

I want to access the "ret_code" to check it equals 100 but if I try:

print (json_response["ret_code"])

I get this error:

TypeError: string indices must be integers

How can I pull the ret_code as an integer?

user892028
  • 175
  • 1
  • 10
  • Have you tried *parsing* the JSON? – jonrsharpe Jan 25 '20 at 16:51
  • Does this answer your question? [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – AMC Jan 25 '20 at 21:48

1 Answers1

1

Thanks jonrsharpe.

I added:

result = json.loads(json_response)

Then I was able to access the ret_code with:

result["ret_code"]
user892028
  • 175
  • 1
  • 10