Quantcast
Viewing all articles
Browse latest Browse all 520

How to conditionally select value with JQ

I am trying to select 1 price if it exists, otherwise I want to select a "default" price which always exists. My JQ works if the RMP price doesn't exist, I just get the 1 value back. But as you can see, if RMP exists I end up with 2 entries ... one with each price. I am only looking for 1 result back. It will either have the RMP price if it exists, or it will have the RET price. Not sure what I'm doing wrong here.

JSON

[  {"PartNumber": "ABC123","Prices": {"Pricing": [        {"@PriceType": "RET","Price": {"#text": "230.3800"          }        },        {"@PriceType": "LST","Price": {"#text": "230.3800"          }        },        {"@PriceType": "RMP","Price": {"#text": "152.0000"          }        }      ]    }  }]

JQ

[.[] | {    sku: .PartNumber,    price: (.Prices?.Pricing? | if type == "array" then .[] else . end | if any(.; ."@PriceType" == "RMP") then select(."@PriceType"? == "RMP").Price."#text" else select(."@PriceType"? == "RET").Price."#text" end),}]

RESULT

[  {"sku": "ABC123","price": "230.3800"  },  {"sku": "ABC123","price": "152.0000"  }]

Viewing all articles
Browse latest Browse all 520

Trending Articles