Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filtering out json object contain certain key in FLASK?

I have a JSON file :

    {
           "id":"xyz",
           "word":"this is word",
           "formats":[
              {
                 "formatId":"xz99",
                 "pengeluaran":"pengeluaran_1",
                 
              },
              {
                 "formatId":"xy100",
                 "pengeluaran":"pengeluaran_2",
                 
              },
              {
                 
                 "ukuran":451563,
                 "formatId":"xy101",
                 "pengeluaran":"pengeluaran_1",
             
            },
        }

in my python.py files, i want to filtering out the JSON object only contain "ukuran" as a key , my expected to getting result like this :

    {
           "id":"xyz",
           "word":"this is word",
           "formats":[
              {
                 
                 "ukuran":451563,
                 "formatId":"xy101",
                 "pengeluaran":"pengeluaran_1",
             
            },
        }

this my python.py code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    def extractJson(appData):    
        appData = json.loads(output)
        
        //list comprehension to filter out only JSON object contain "ukuran"?

        print(appData)

Thanks in advance.

>Solution :

It could look something like this:

This example uses list comprehension to filter out the correct format by checking to see if the format contains the key "ukuran", and then assigning the filtered list back "formats" key for the object.

def extractJson(output):    
    appData = json.loads(output)
    appData["formats"] = [
       format for format in appData["formats"] if "ukuran" in format
    ]
    print(appData)
    return appData
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading