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: 16)
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
  how can I display only desired items? 3lnyn0 5 1,160 Dec-25-2021, 06:49 PM
Last Post: menator01
  ERROR: importing desired module mbgamer28 0 1,251 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  how to filter two fields in json using python python_student 4 2,350 Mar-15-2021, 05:58 PM
Last Post: python_student
  Not rounding to desired decimal places? pprod 2 1,797 Mar-05-2021, 11:11 AM
Last Post: pprod
  Why this code not getting desired output ? MDRI 2 1,940 Sep-18-2020, 02:11 AM
Last Post: MDRI
  showing only desired part of a plot grknkilicaslan 1 1,553 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  Not Getting the Desired value using json.dumps saurabh210 0 1,131 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  Invalid JSON payload received. Unknown name “”: Root element must be a message." hellraiser 4 10,285 Aug-18-2019, 03:41 PM
Last Post: hellraiser
  Capturing inputs values from internal python script limors11 11 3,634 Jun-16-2019, 05:05 PM
Last Post: DeaD_EyE
  Capturing a snapshot from the video sreeramp96 1 1,723 May-24-2019, 07:02 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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