Python Forum
Json fields not being retrieved
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json fields not being retrieved
#1
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"
        }
    ]
}
Reply
#2
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.
Reply
#3
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']
Reply
#4
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"
},
(…)
Reply
#5
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR
  Compare fields from two csv files georgebijum 3 1,335 Apr-25-2022, 11:16 PM
Last Post: Pedroski55
  how to filter two fields in json using python python_student 4 3,606 Mar-15-2021, 05:58 PM
Last Post: python_student
  Mapping Fields zak308 0 2,467 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020