英文:
If statement within a JSON selection?
问题
在下面的JSON中,我尝试在is_rpp = True
时选择原始价格值。有时这可以是第一个价格,也可以是列出的任何N个价格中的任何一个,但大多数情况下都没有或只有一个is_rpp = True
价格。
这是否可能?
例如,使用{{ $json["price"]["value"] }}确实为我选择了价格,但我想要is_rpp = True
价格。
谢谢
{
"position": 1,
"title": "Apple USB-C to Lightning Cable (2 m)",
"asin": "asintest",
"link": "https://www.amazon.com/",
"categories": [{
"name": "Electronics",
"id": "electronics"
}],
"image": "https://m.media-amazon.com/images/",
"is_prime": true,
"rating": 4.8,
"ratings_total": 15213956,
"prices": [{
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
{
"symbol": "$",
"value": 35,
"currency": "USD",
"raw": "$35.00",
"name": "$29.00",
"is_rrp": true
}
],
"price": {
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
"page": "1",
"position_overall": 1
}
这将在N8N中使用:https://docs.n8n.io/code-examples/expressions/jmespath/#an-alternative-to-arrow-functions
英文:
In the JSON below, I am attempting to select the raw price value when the price is_rpp = True. This can sometimes be the first price or it can be any of N number of prices listed but most of the time there is none or ONE is_rpp = True price.
Is this possible?
As an example, using {{ $json["price"]["value"] }} does select the price for me but I want the is_rpp = True price.
Thank you
{
"position": 1,
"title": "Apple USB-C to Lightning Cable (2 m)",
"asin": "asintest",
"link": "https://www.amazon.com/",
"categories": [{
"name": "Electronics",
"id": "electronics"
}],
"image": "https://m.media-amazon.com/images/",
"is_prime": true,
"rating": 4.8,
"ratings_total": 15213956,
"prices": [{
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
{
"symbol": "$",
"value": 35,
"currency": "USD",
"raw": "$35.00",
"name": "$29.00",
"is_rrp": true
}
],
"price": {
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
"page": "1",
"position_overall": 1
}
This will be used within N8N: https://docs.n8n.io/code-examples/expressions/jmespath/#an-alternative-to-arrow-functions
答案1
得分: 2
这是使用 Array.prototype.find
函数可以执行的操作:
如果它返回 undefined
,意味着在 prices
数组中没有满足 is_rpp = true
条件的项目。
$json.prices?.find(p => p.is_rrp)?.value
以下是 JavaScript 代码示例:
const $json = {
"position": 1,
"title": "Apple USB-C to Lightning Cable (2 m)",
"asin": "asintest",
"link": "https://www.amazon.com/",
"categories": [{
"name": "Electronics",
"id": "electronics"
}],
"image": "https://m.media-amazon.com/images/",
"is_prime": true,
"rating": 4.8,
"ratings_total": 15213956,
"prices": [{
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
{
"symbol": "$",
"value": 35,
"currency": "USD",
"raw": "$35.00",
"name": "$29.00",
"is_rrp": true
}
],
"price": {
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
"page": "1",
"position_overall": 1
};
const rrpPrice = $json.prices?.find(p => p.is_rrp)?.value;
console.log(rrpPrice); // 35
希望这对你有帮助。
英文:
Here's what you can do using Array.prototype.find
function:
If it returns undefined
, meaning there's no items with is_rpp = true
condition met inside of prices
array.
$json.prices?.find(p => p.is_rrp)?.value
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const $json = {
"position": 1,
"title": "Apple USB-C to Lightning Cable (2 m)",
"asin": "asintest",
"link": "https://www.amazon.com/",
"categories": [{
"name": "Electronics",
"id": "electronics"
}],
"image": "https://m.media-amazon.com/images/",
"is_prime": true,
"rating": 4.8,
"ratings_total": 15213956,
"prices": [{
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
{
"symbol": "$",
"value": 35,
"currency": "USD",
"raw": "$35.00",
"name": "$29.00",
"is_rrp": true
}
],
"price": {
"symbol": "$",
"value": 29,
"currency": "USD",
"raw": "$29.00",
"name": "$29.00",
"is_primary": true
},
"page": "1",
"position_overall": 1
};
const rrpPrice = $json.prices?.find(p => p.is_rrp)?.value;
console.log(rrpPrice); // 35
<!-- end snippet -->
答案2
得分: 0
这将在prices数组中的is_rrp为true时提供原始价格值。
请注意,JSONPath表达式可能因特定实现而有所不同。
英文:
{{ $json.prices[?(@.is_rrp == true)].raw }}
This will give you the raw price value when is_rrp is true in the prices array.
Note that JSONPath expressions may vary depending on the specific implementation
答案3
得分: 0
Got it!
{{ $jmespath($json.prices, "[?is_rrp == true
] | [0].value") }}
英文:
Got it!
{{ $jmespath($json.prices, "[?is_rrp == `true`] | [0].value") }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论