Python Check if Key/Value exists in JSON output

Tags: , ,



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

[
   {
      "domain":"www.slatergordon.co.uk",
      "displayed_link":"https://www.slatergordon.co.uk/",
      "description":"We Work With Thousands Of People Across The UK In All Areas Of Personal Legal Services. Regardless Of How You Have Been Injured Through Negligence We're Here To Help You. Personal Injury Experts.",
      "position":1,
      "block_position":"top",
      "title":"Car Claims Solicitors - No Win No Fee Solicitors - SlaterGordon.co.uk",
      "link":"https://www.slatergordon.co.uk/personal-injury-claim/road-traffic-accidents-solicitors/",
      "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABABGgJwdg&sig=AOD64_3u1ct0jmXAnvemxFHh_tfK5UK8Xg&q&adurl"
   },
   {
      "is_phone_ad":true,
      "phone_number":"0333 358 0496",
      "domain":"www.accident-claimsline.co.uk",
      "displayed_link":"http://www.accident-claimsline.co.uk/",
      "description":"Car Insurance Claims Advice - Car Accident Claims Helpline",
      "sitelinks":[
         {
            "title":"Replacement Vehicle Hire",
            "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABALGgJwdg&ae=2&sig=AOD64_20YjAoyMY_c6XVTnBU1vQAD2tDTA&q=&ved=2ahUKEwjvlM-djLDwAhVmJzQIHSZHDLEQvrcBegQIBRAM&adurl="
         },
         {
            "title":"Request a Call Back",
            "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABAOGgJwdg&ae=2&sig=AOD64_36-Pd831AXrPbh1yvUyTbhXH2irg&q=&ved=2ahUKEwjvlM-djLDwAhVmJzQIHSZHDLEQvrcBegQIBRAN&adurl="
         }
      ],
      "position":6,
      "block_position":"bottom",
      "title":"Car Insurance Claims Advice - Car Accident Claims Helpline",
      "link":"http://www.accident-claimsline.co.uk/",
      "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABAGGgJwdg&ae=2&sig=AOD64_09pMtWxFo9s8c1dL16NJo5ThOlrg&q&adurl"
   }
]

JSON Blob 2

JSON

[
   {
      "domain":"www.slatergordon.co.uk",
      "displayed_link":"https://www.slatergordon.co.uk/",
      "description":"We Work With Thousands Of People Across The UK In All Areas Of Personal Legal Services. Regardless Of How You Have Been Injured Through Negligence We're Here To Help You. Personal Injury Experts.",
      "position":1,
      "block_position":"top",
      "title":"Car Claims Solicitors - No Win No Fee Solicitors - SlaterGordon.co.uk",
      "link":"https://www.slatergordon.co.uk/personal-injury-claim/road-traffic-accidents-solicitors/",
      "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABABGgJwdg&sig=AOD64_3u1ct0jmXAnvemxFHh_tfK5UK8Xg&q&adurl"
   },
   {
      "is_phone_ad":true,
      "phone_number":"0333 358 0496",
      "domain":"www.accident-claimsline.co.uk",
      "displayed_link":"http://www.accident-claimsline.co.uk/",
      "description":"Car Insurance Claims Advice - Car Accident Claims Helpline",
      "sitelinks":[
         {
            "title":"Replacement Vehicle Hire",
            "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABALGgJwdg&ae=2&sig=AOD64_20YjAoyMY_c6XVTnBU1vQAD2tDTA&q=&ved=2ahUKEwjvlM-djLDwAhVmJzQIHSZHDLEQvrcBegQIBRAM&adurl="
         },
         {
            "title":"Request a Call Back",
            "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABAOGgJwdg&ae=2&sig=AOD64_36-Pd831AXrPbh1yvUyTbhXH2irg&q=&ved=2ahUKEwjvlM-djLDwAhVmJzQIHSZHDLEQvrcBegQIBRAN&adurl="
         }
      ],
      "position":6,
      "block_position":"top",
      "title":"Car Insurance Claims Advice - Car Accident Claims Helpline",
      "link":"http://www.accident-claimsline.co.uk/",
      "tracking_link":"https://www.google.co.uk/aclk?sa=l&ai=DChcSEwj8-NSdjLDwAhXBEH0KHRYwA1MYABAGGgJwdg&ae=2&sig=AOD64_09pMtWxFo9s8c1dL16NJo5ThOlrg&q&adurl"
   }
]

Desired Output

if "block_position":"bottom" in JSONBlob:
    do something
else:
    do something else

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

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:

if any([obj.get('block_position') == 'bottom' for obj in JSONBlob]):
    print('I do something')
else:
    print('I do somehting else')

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:

for obj in JSONBlob:
    if obj.get('block_position') == 'bottom':
        print('I do something with the object')
    else:
        print('I do something else with the object')

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:

for obj in JSONBlob:
    if obj.get('block_position') == 'bottom':
        print('I do something with the object')


Source: stackoverflow