Not able to get indexOf working in DataAction result

I have a DataAction that can return the following result:

{
"entities": [
{
"LongName": "Here is a long name",
"key": "long1",
"Classes": "A,B,C"
},
{
"LongName": "Here is a long name",
"key": "long2",
"Classes": "D,E,F"
}
],
"pageSize": 25,
"pageNumber": 1,
"total": 2,
"pageCount": 1
}

I have a translationMap for the result that looks like this:

{
"translationMap": {
"result": "$.entities[?(@.LongName == 'AMCAP Fund' && @.Classes.indexOf('D') != -1)].key"
},
"translationMapDefaults": {
"result": "[]"
},
"successTemplate": "{"entities.key": ${result}}"
}

With the hope that entities.key will return "long2"

But I keep getting nothing. If I remove the "indexOf" check, it returns the key for the first entry. But I need it to return only the key where the "D" is within the Classes.

Am I using indexOf incorrectly? Or is there another way to accomplish this?

Follow-up question:
If so, is there also a way to do an indexOf a whole word, so that if I had two comma-delimited lists, "A,B,C", and "AA,BB,CC", it would return 0 for the first one, -1 for the second one, if looking for the indexOf 'A'

I also tried a jsonpath tool, and it seems this jsonpath should work:

$.entities[?(@.LongName == 'Here is a long name' && @.Classes.indexOf('D') != -1)].key

Returns back long2 in the evaluator. But in the Data Action, it is empty.

Evaluator:
image
image

Data Action test

Well, I just discovered that input parameters can't be used in translationMaps.

That's unfortunate. So even if I could get this jsonpath to work, it wouldn't matter.

I went ahead and voted for the "idea", but it looks to be 3 years old. Nothing has happened in that regard.

It would be good to still get a resolution to this jsonpath issue.

I think you want to use one of the filter operations, not the indexOf function. I can't speak to why the online parser returns anything at all, as indexOf isn't even listed as a valid function for the Jayway JSONPath implementation (GitHub - json-path/JsonPath: Java JsonPath implementation). I guess that's a feature difference with the Goesner implementation, which is what appears to be leveraged in the tester tool you linked to.

I've performed similar operations to what you're trying to accomplish using the "in" filter. So something like:

$.entities[?(@.LongName == 'Here is a long name' && @.Classes in ['D'])].key

You might need to use "anyOf", since @.Classes would return an array, and you're looking for 'D' to exist in the array (as opposed to the inverse, where Classes was a string and you were wanting to find a match in an array), but the concept is still the same.

also, as a tester tool, this is what I've found for Jayway implementations: https://www.javainuse.com/jsonpath Doesn't explicitly state that it's using Jayway under the covers, but it seems to work well and the operators appear to match what's documented in the Jayway github.

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.