Python Forum
Json fields not being retrieved - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Json fields not being retrieved (/thread-29629.html)



Json fields not being retrieved - mrcurious2020 - Sep-14-2020

I am trying to read a json file using the below. I am unable to retrieve values though. I'd appreciate a correction to the code below please.

import json
import sys
import os

    content  = None
    with open("s.json", "r") as f:
        content = list(json.load(f))
    
    #build a list of values of location, name and state:
    for entry in content:
        tmp_list = [entry.get('location'), entry.get('name'), entry.get('state')]
        print(tmp_list)


#file:s.json

{
    "environment-sensor": [
        {
            "current-reading": 0,
            "high-critical-threshold": 0,
            "high-normal-threshold": 0,
            "location": "P0",
            "low-critical-threshold": 0,
            "low-normal-threshold": 0,
            "name": "PEM Iout",
            "sensor-name": "current",
            "sensor-units": "amperes",
            "state": "Normal"
        }
    ]
}



RE: Json fields not being retrieved - scidam - Sep-14-2020

What type of error did you get? Indentation error?

I just tried:

s ="""{
    "environment-sensor": [
        {
            "current-reading": 0,
            "high-critical-threshold": 0,
            "high-normal-threshold": 0,
            "location": "P0",
            "low-critical-threshold": 0,
            "low-normal-threshold": 0,
            "name": "PEM Iout",
            "sensor-name": "current",
            "sensor-units": "amperes",
            "state": "Normal"
        }
    ]
}"""
json.loads(s)
and it works fine.


RE: Json fields not being retrieved - bowlofred - Sep-14-2020

Lines 5-7 are indented improperly.

As the outer json object is a mapping, that loads into python as a dict. When you run list() on it, that returns only the keys of that dict (and there's only one key).

The way you parse the JSON depends on the actual data. The dict you are interested in is inside a list, inside another dict.

    content = json.load(f)['environment-sensor']



RE: Json fields not being retrieved - mrcurious2020 - Sep-14-2020

Thank you.
How about the json output below. Can you clarify this below, if possible:
content = json.load(f)['device-hardware']  <== This is not functional for the below
{
"device-hardware": { <== I see this is a dictionary
"device-alarm": [ <== this would be the key, but there is a list
{
"alarm-category": "alarm-severity-critical",
"alarm-description": "Power Supply Failure",
"alarm-id": 3,
"alarm-instance": 0,
"alarm-name": "Power Supply Module 0",
"alarm-time": "2020-06-30T05:35:18+00:00"
},
(…)


RE: Json fields not being retrieved - bowlofred - Sep-14-2020

The outer object is a dictionary.
The key "device-hardware" has a another dictionary as its value.
The key "device-alarm" has a list as its value.
The first element of the list is a dictionary.
One of the keys in that dictionary is "alarm-category"

So...
data['device-hardware']['device-alarm'][0]['alarm-category'] == 'alarm-severity-critical'