skip to Main Content

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
}

2

Answers


  1. 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
    
    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
    Login or Signup to reply.
  2. {{ $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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search