Python Forum
Json filter is not capturing desired key/element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json filter is not capturing desired key/element
#1
Hello, I am trying to do the following:

1) Read the Json file (sample output attached).
2) If account ID is 4254254 AND "compliance": "Fail", I need to print the respective dictionary.

This is what I have done so far, but it is not working (error explained below):

import json

RAW_FILE = "to.json"
 
def scanvuln(file,account):

    # with open (file, 'r') as f:
    f = open(file, 'r')    
    data = json.load(f)
    for element in data:
        print(element)  #<== it does print the respective account that are in the json file. OK. 
        if data[account]["cloudformation"]["CloudFormation Drift Detection Analysis"]["analysis_results"][0]["compliance"] == "Fail":
                print(element) #<== This is simply not working. It does not capture the key/value. Goal is that if that if compliace== "Fail", I should print
                                      #the respective section of the dictionary for further analysis.
 
                                                                                                        
myaccount = "4254254"
scanvuln(RAW_FILE, myaccount)
Help is appreciated.

Attached Files

.json   to.json (Size: 3.21 KB / Downloads: 119)
Reply
#2
If you don't understand what your program is doing, always add print statements to see the progress in more detail. In this case you wonder why your "if" statement is not working. Just print the condition before the "if":
print(data[account]["cloudformation"]["CloudFormation Drift Detection Analysis"]["analysis_results"][0]["compliance"])
Output:
Pass
As "Pass" is not "Fail" nothing is printed.

Also it is strange you iterate over the elements (in your example there is only one element at toplevel) but you test always the same element: "data[account]". I think you want to test "data[element]".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Capturing BS4 values into DF and writing to CSV cubangt 18 1,948 Sep-05-2023, 01:57 PM
Last Post: cubangt
  capturing multiline output for number of parameters jss 3 809 Sep-01-2023, 05:42 PM
Last Post: jss
  how can I display only desired items? 3lnyn0 5 2,022 Dec-25-2021, 06:49 PM
Last Post: menator01
  ERROR: importing desired module mbgamer28 0 1,674 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  how to filter two fields in json using python python_student 4 3,663 Mar-15-2021, 05:58 PM
Last Post: python_student
  Not rounding to desired decimal places? pprod 2 2,545 Mar-05-2021, 11:11 AM
Last Post: pprod
  Why this code not getting desired output ? MDRI 2 2,522 Sep-18-2020, 02:11 AM
Last Post: MDRI
  showing only desired part of a plot grknkilicaslan 1 2,330 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  [Beginner] Code is not producing desired result fakej171 2 2,423 Mar-21-2020, 10:26 AM
Last Post: buran
  Not Getting the Desired value using json.dumps saurabh210 0 1,500 Feb-03-2020, 06:48 PM
Last Post: saurabh210

Forum Jump:

User Panel Messages

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