简体   繁体   中英

Picking an array of hashes from a hash

I have a hash coming back from an XML datasource that looks like this:

{...,
  'records' :{
    'record' :[
      {'availability' :{'$t' :'available'}, ...},
      {'availability' :{'$t' :'available'}, ...}
    ]
  }
};

I'd like to get all the record hashes into an array so I can filter() it and do some other operations. However, when I have this statement in my pre block,

raw_records = raw.pick("$..record");

the array that gets returned is an array of two empty strings:

var raw_records = ['', ''];

The odd thing is that I can pick out just availability with expected results:

availability = raw.pick("$..availability.$t");

producing

var availability = ['available', 'available'];

What's wrong with my first pick() ?

EDIT: Here is a more complete version that should help with reproducing the problem. It's slightly different, since I'm using the JSON version of the web service now:

global {
    datasource hbll <- "https://svc.lib.byu.edu/services/catalog/v1/search/?field=isbn&format=json&terms=";
}

rule new_rule {
    select when pageview "amazon.com/.*/?dp/(.*)/" setting (isbn)
    pre {
        //This is the array with two empty strings...
        raw = datasource:hbll(isbn);
        myfilter = function(x) { x.pick("availability") eq "available"; };
        records = raw.filter(myfilter);
        len = records.length();
        availability = records.pick("$..availability");
        middleman = len > 1 => availability[0] | availability;
        available = middleman eq "available" => true | false;
        url_list = records.pick("$..url");
        url = len > 1 => url_list[0] | url_list;
        msg = <<
            <p>This book is available for checkout at the BYU Library.</p>
            <a href="#{url}">More information</a>
        >>;
    }
    notify("BYU Harold B. Lee Library", msg) with sticky=true;
}

I'm going to need a more complete example. The test app and results I got are below:

ruleset a8x167 {
    meta {
        name "Pick - Array of Hashes"
        description <<
            Testing
        >>
        author "Sam Curren"
        logging on
    }

    dispatch {}

    global {
        raw = {
          'records' :{
            'record' :[
              {'availability' :{'$t' :'available'}},
              {'availability' :{'$t' :'available'}}
            ]
          }
        };

    }

    rule test {
        select when pageview ".*" setting ()
        pre {
            raw_records = raw.pick("$..record");
            availability = raw.pick("$..availability.$t");
        }
        notify("Hello World", "This is a sample rule.");
    }
}

And Results:

var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM