Friday, 7 January 2022

Python - Itterate down dictionairy - move down tree conditionally

I have a some python code below that walk down a tree but I want it to work down a tree checking taking some paths conditioally based on values. I want to get the LandedPrice for branches of tree based on condition and fulfillmentChannel

parsed_results['LowestLanded'] = sku_multi_sku['Summary']['LowestPrices']['LowestPrice']['LandedPrice']['Amount']['value']

That walks down this tree but values because there are two LowestPrice records/dicts returned one for each condition and fulfillmentChannel pair. I want to filter on condition=new and fulfillmentChannel=Amazon so I only get back one record. When I parse XML data I can do it with code similar to LowestPrices/LowestPrice[@condition='new'][@fulfillmentChannel='Merchant']/LandedPrice/Amount" but couldn't get similar code to work here. How do I do this with dictionaries?

 "LowestPrices":{
     "value":"\n                ",
     "LowestPrice":[
        {
           "value":"\n                    ",
           "condition":{
              "value":"new"               #condtion new
           },
           "fulfillmentChannel":{
              "value":"Amazon"            ## fulfilllmentChannel #1
           },
           "LandedPrice":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"19.57"
              }
           },
           "ListingPrice":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"19.57"
              }
           },
           "Shipping":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"0.00"
              }
           }
        },
        {
           "value":"\n                    ",
           "condition":{
              "value":"new"
           },
           "fulfillmentChannel":{
              "value":"Merchant"
           },
           "LandedPrice":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"19.25"
              }
           },
           "ListingPrice":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"19.25"
              }
           },
           "Shipping":{
              "value":"\n                        ",
              "CurrencyCode":{
                 "value":"USD"
              },
              "Amount":{
                 "value":"0.00"
              }
           }
        }
     ]
  },


from Python - Itterate down dictionairy - move down tree conditionally

No comments:

Post a Comment