If语句在JSON选择内部?

huangapple go评论75阅读模式
英文:

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 = {
    &quot;position&quot;: 1,
    &quot;title&quot;: &quot;Apple USB-C to Lightning Cable (2 m)&quot;,
    &quot;asin&quot;: &quot;asintest&quot;,
    &quot;link&quot;: &quot;https://www.amazon.com/&quot;,
    &quot;categories&quot;: [{
        &quot;name&quot;: &quot;Electronics&quot;,
        &quot;id&quot;: &quot;electronics&quot;
    }],
    &quot;image&quot;: &quot;https://m.media-amazon.com/images/&quot;,
    &quot;is_prime&quot;: true,
    &quot;rating&quot;: 4.8,
    &quot;ratings_total&quot;: 15213956,
    &quot;prices&quot;: [{
            &quot;symbol&quot;: &quot;$&quot;,
            &quot;value&quot;: 29,
            &quot;currency&quot;: &quot;USD&quot;,
            &quot;raw&quot;: &quot;$29.00&quot;,
            &quot;name&quot;: &quot;$29.00&quot;,
            &quot;is_primary&quot;: true
        },
        {
            &quot;symbol&quot;: &quot;$&quot;,
            &quot;value&quot;: 35,
            &quot;currency&quot;: &quot;USD&quot;,
            &quot;raw&quot;: &quot;$35.00&quot;,
            &quot;name&quot;: &quot;$29.00&quot;,
            &quot;is_rrp&quot;: true
        }
    ],
    &quot;price&quot;: {
        &quot;symbol&quot;: &quot;$&quot;,
        &quot;value&quot;: 29,
        &quot;currency&quot;: &quot;USD&quot;,
        &quot;raw&quot;: &quot;$29.00&quot;,
        &quot;name&quot;: &quot;$29.00&quot;,
        &quot;is_primary&quot;: true
    },
    &quot;page&quot;: &quot;1&quot;,
    &quot;position_overall&quot;: 1
};

const rrpPrice = $json.prices?.find(p =&gt; p.is_rrp)?.value;

console.log(rrpPrice);  // 35

<!-- end snippet -->

答案2

得分: 0

这将在prices数组中的is_rrptrue时提供原始价格值。

请注意,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, &quot;[?is_rrp == `true`] | [0].value&quot;) }}

huangapple
  • 本文由 发表于 2023年6月5日 09:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定