-1

I have a packet and I need to split it so that I can extract information from it. The packet is in string format and looks like this:

{"S.No":"9","data":[{"id":"ID1","value":" 0.34"},{"id":"ID2","value":" 2.92"},{"id":"ID3","value":" 2.92"}]}

From the above packet I need to extract the values of the id like for ID1 value 0.34

How can I extract all the values.

Thanks

Roars
  • 623
  • 5
  • 17
anna carolina
  • 209
  • 1
  • 10

1 Answers1

4

If I understood correctly you can try something like:

import json
string_data = '{"S.No":"9","data":[{"id":"ID1","value":" 0.34"},{"id":"ID2","value":" 2.92"},{"id":"ID3","value":" 2.92"}]}'
data = json.loads(string_data)
[float(d['value']) for d in data['data']]
# will result in:
[0.34, 2.92, 2.92]
pedromcaraujo
  • 307
  • 1
  • 4