I have two questions about extending this. I would like to limit the results to only a list of OCLC symbols and I would like to count the number of results. Can I do this all in one statement or do I need multiple passes? I was able to get a filter to work with a single value, but not multiple values.
Hi @christie! For the filtering part, are you saying that you only want to return an OCLC symbol if it's part of a specific set of OCLC symbols? If so, I'm not sure how one would do that. However, I do think it'd be useful to try and introduce a dedicated filter GREL function.
As for finding the number of elements, I think you should be able to use length() after the call to forEach. I don't think the same expression would be able to return both the joined list of symbols and the total count of symbols, so you might need to do two passes to get both values.
I hope that's helpful, and my apologies if I've misinterpreted your question!
You have not misinterpreted my question at all. And, I thank you for you suggestion. I have tried a filter in a number of incarnations. The only examples I could find that use a filter with include using creating a named list. Nothing has worked and it could be because I have never used a named list nor have I used a filter before. I will not include all of my attempts, but I have tried both of these with no luck. (The list is actually much longer, but I truncated it for readability.)
Example 1:
let symbolList = ["CGU","UGC"];
forEach(value.parseJson().briefRecords, r, forEach(r.institutionHolding.briefHoldings.filter(h, symbolList.join('|').contains(h.oclcSymbol)).length()
Example 2:
let list = ["CGU","UGC"];
forEach(value.parseJson().briefRecords, r, forEach(r.institutionHolding.briefHoldings, h, h.filter(h -> list.indexOf(h.oclcSymbol) > -1).length()
Or you could use Jython which reduces some of the nesting.
import json
symbolList = {"CGU", "UGC"}
records = json.loads(value)["briefRecords"]
l = []
for record in records:
count = 0
for holding in record["institutionHolding"]["briefHoldings"]:
if holding in symbolList:
count += 1
l.append(str(count))
return ",".join(l)