我正在try Select 1个价格,如果它存在,否则我想 Select 一个始终存在的"默认"价格.如果RMP价格不存在,我的JQ会工作,我只会拿回1的值.但正如你所看到的,如果RMP存在,我最终会得到2个条目...每种价格都有一个.我只想找一个结果回来.它要么具有RMP价格(如果存在),要么具有RET价格.不知道我到底做错了什么.
杰森
[
{
"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),
}]
结果
[
{
"sku": "ABC123",
"price": "230.3800"
},
{
"sku": "ABC123",
"price": "152.0000"
}
]