Skip to content
Advertisement

Python Check if Key/Value exists in JSON output

I have a JSON output and I want to create an IF statement so if it contains the value I am looking for the do something ELSE do something else.

JSON Blob 1

JavaScript

JSON Blob 2

JavaScript

Desired Output

JavaScript

but I cant seem to get it to trigger for me. I want it to search through the entire output and if it contains that key/value do something and if it doesnt contain it do something else.

Blob 1 would go down the IF path

Blob 2 would go down the else path

Advertisement

Answer

The main problem you have here is that the JSON output is a list/array with two objects inside. As you can have the block_position key in any of the inner objects, you could do something like this:

JavaScript

EDIT 1: OK, I think I got your point. You only need to do something for each object with block_position set to bottom. Then the following should do it:

JavaScript

EDIT 2: As spoken in the post, if you only want to do something with the objects with block_position set as bottom, you can suppress the else clause as follows:

JavaScript
Advertisement