0

I have the following JSON data:

{
  "status": {
    "status_code": 0,
    "status_message": "SUCCESS"
  },
  "device_id": 89911454,
  "type": "obs_st",
  "source": "cache",
  "summary": {
    "pressure_trend": "falling",
    "strike_count_1h": 0,
    "strike_count_3h": 0,
    "precip_total_1h": 0.0,
    "precip_accum_local_yesterday": 0.0,
    "precip_accum_local_yesterday_final": 0.0,
    "precip_analysis_type_yesterday": 1,
    "feels_like": 33.3,
    "heat_index": 33.3,
    "wind_chill": 33.3
  },
  "obs": [
    [1600214324, 0, 0, 0, 0, 3, 1012.8, 33.3, 35, 27898, 1.94, 232, 0, 0, 0, 0, 2.56, 1, 0, null, null, 0]
  ]
}

I only want the value for "heat_index". In this case that "heat_index" = 33.3. I've not been able to figure out how. Can anyone give me a hand with this? Thanks for any help provided!

3
  • Doesn't myJson['summary']['heat_index'] work? Commented Sep 16, 2020 at 0:51
  • Thanks for the tip, but whenever I do something like that I get this error message: TypeError: string indices must be integers Commented Sep 16, 2020 at 0:59
  • 1
    Yes, it does work if I do this first: json_data = json.loads(result) Thanks! Commented Sep 16, 2020 at 1:01

1 Answer 1

1

If you have a json object stored in string format you need to decode it before you can access it using the "[ ]" operators. Luckily Python as usual already has a library for this. The json library has the ability to encode, decode and pretty print json data.

import json

file = open("your-json-file-here.json")
jsonString = file.read()
file.close()

jsonObject = json.loads(jsonString)
print(jsonObject["summary"]["heat_index"])

It doesn't matter too much how you get the json string as long as you call the loads function on it.

Sign up to request clarification or add additional context in comments.

Comments

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.