jsonpathkrl

Using jsonPath looking for a string


I'm trying to use jsonPath and the pick function to determine if a rule needs to run or not based on the current domain. A simplified version of what I'm doing is here:

    global 
{
    dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}

rule checkdataset is active
{
    select when pageview ".*" setting ()
    pre
    {
        merchantData = shopscotchMerchants.pick("$.merchants[?(@.merchant=='Telefora')]");
    }
    emit 
    <|
        console.log(merchantData);
    |>
}

The console output I expect is the telefora object, instead I get all three objects from the json file.

If instead of merchant=='Telefora' I use merchantID==16 then it works great. I thought jsonPath could do matches to strings as well. Although the example above isn't searching against the merchantDomain part of the json, I'm experiencing the same problem with that.


Solution

  • Your problem comes from the fact that, as stated in the documentation, the string equality operators are eq, neq, and like. == is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of the eq string equality operator.

    Simply swap == for eq in you JSONpath filter expression and you will be good to go:

        global 
    {
        dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
    }
    
    rule checkdataset is active
    {
        select when pageview ".*" setting ()
        pre
        {
            merchantData = shopscotchMerchants.pick("$.merchants[?(@.merchant eq 'Telefora')]"); // replace == with eq
        }
        emit 
        <|
            console.log(merchantData);
        |>
    }
    

    I put this to the test in my own test ruleset, the source for which is below:

    ruleset a369x175 {
      meta {
        name "test-json-filtering"
        description <<
    
        >>
        author "AKO"
        logging on
      }
    
      dispatch {
          domain "exampley.com"
      }
    
      global {
        dataset merchant_dataset <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
      }
    
      rule filter_some_delicous_json {
        select when pageview "exampley.com"
        pre {
            merchant_data = merchant_dataset.pick("$.merchants[?(@.merchant eq 'Telefora')]");
        }
        {
            emit <|
                try { console.log(merchant_data); } catch(e) { }
            |>;
        }
      }
    }