简体   繁体   中英

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

[
   {
      "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

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')

you can use JMESPath library. Its a query language for JSON. Basic jmespath expression for your case would be [?block_position== bottom ]. This will filter out the specific node for you. I tried it online here with data provided by you. If you are looking for more nested node you will only have to alter your expression to search that specific node.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM