Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem to parse a json
#1
Hi

I've some problem to parse my json
which is like:

{
    "Results": {
        "check1": {
            "LastResultTime": 1607002708,
            "LastResultTimeH": "2020-12-03 14:38:28 +0100 CET",
            "Status": 0,
            "StatusH": "OK",
            "Origin": "1"
        },
        "check2": {
            "LastResultTime": 1607002934,
            "LastResultTimeH": "2020-12-03 14:42:14 +0100 CET",
            "Status": 0,
            "StatusH": "KO",
            "Origin": "1"
        },
        "check3": {
            "LastResultTime": 1607002934,
            "LastResultTimeH": "2020-12-03 14:42:14 +0100 CET",
            "Status": 2,
            "StatusH": "CRITICAL",
            "Origin": "2"
        }
    },
    "Status": 2,
    "StatusH": "CRITICAL",
    "Maintenance": {
        "Reason": "Ok",
        "Status": 0,
        "StatusH": "STATE_OK",
        "When": "2020-12-03 14:42:14.377955151 +0100 CET m=+0.000776843"
    },
}
I want to parse this json with python and retrieve values like:
Check1 / StatusH / OK
Check2 / StatusH / KO
check3 / StatusH / CRITICAL
etc...
I'm trying with
data = json.loads(json_data.decode(), parse_float=float)
for i in data['Results']:
    for a, b in data.items():
        if (a == "StatusH"):
            print (i,b)
But it doesnt work:
check1 CRITICAL
check2 CRITICAL
check3 CRITICAL
If someone has an idea..
Thanks for any help

Alex
Reply
#2
I think I' ve found the solution:

for i in data['Results']:
    for a,b in data['Results'].items():
        if (a == i):
            print (i, b["StatusH"])
Reply
#3
what you have is a dictionary, not json.
where is the data stored?
is it in a file which claims to be json? You don't show enough code to know.
at any rate, if you named the dictionary, then you could access it
example:
mydict = {
    "Results": {
        "check1": {
            "LastResultTime": 1607002708,
            "LastResultTimeH": "2020-12-03 14:38:28 +0100 CET",
            "Status": 0,
            "StatusH": "OK",
            "Origin": "1"
        },
        "check2": {
            "LastResultTime": 1607002934,
            "LastResultTimeH": "2020-12-03 14:42:14 +0100 CET",
            "Status": 0,
            "StatusH": "KO",
            "Origin": "1"
        },
        "check3": {
            "LastResultTime": 1607002934,
            "LastResultTimeH": "2020-12-03 14:42:14 +0100 CET",
            "Status": 2,
            "StatusH": "CRITICAL",
            "Origin": "2"
        }
    },
    "Status": 2,
    "StatusH": "CRITICAL",
    "Maintenance": {
        "Reason": "Ok",
        "Status": 0,
        "StatusH": "STATE_OK",
        "When": "2020-12-03 14:42:14.377955151 +0100 CET m=+0.000776843"
    },
}

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

display_dict(mydict)
running produces:
Output:
Results check1 LastResultTime: 1607002708 LastResultTimeH: 2020-12-03 14:38:28 +0100 CET Status: 0 StatusH: OK Origin: 1 check2 LastResultTime: 1607002934 LastResultTimeH: 2020-12-03 14:42:14 +0100 CET Status: 0 StatusH: KO Origin: 1 check3 LastResultTime: 1607002934 LastResultTimeH: 2020-12-03 14:42:14 +0100 CET Status: 2 StatusH: CRITICAL Origin: 2 Status: 2 StatusH: CRITICAL Maintenance Reason: Ok Status: 0 StatusH: STATE_OK When: 2020-12-03 14:42:14.377955151 +0100 CET m=+0.000776843
the code on line 37 does all of the iteration.
Note that the function display_dict is recursive, so nested dictionaries call display_dict in turn.
for key, value in dictname.items()
enigma619 likes this post
Reply
#4
Thanks for your detailed answer!
My dictionary is a result of a curl https://....
With your answer I will succeed to retrieve all my values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 725 Nov-14-2023, 11:34 PM
Last Post: snippsat
  [split] Parse Nested JSON String in Python mmm07 4 1,518 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Trying to parse only 3 key values from json file cubangt 8 3,442 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Problem with nested JSON Kalet 7 2,780 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Problem with Json Array kwekey 2 1,665 Aug-02-2021, 05:11 PM
Last Post: kwekey
  How to parse JSON DIC? ogautier 4 2,226 Sep-15-2020, 06:03 PM
Last Post: ogautier
  Parse JSON multiple objects larkin_L 8 5,691 May-27-2020, 11:18 AM
Last Post: nuffink
  How can i parse a log file to JSON. menarcarlos 2 2,421 May-26-2020, 10:23 AM
Last Post: buran
  json problem enigma619 9 3,675 Dec-19-2019, 08:29 AM
Last Post: enigma619
  Output to a json file problem Netcode 3 3,717 Nov-22-2019, 01:44 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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