Legacy Dev Forum Posts

 View Only

Sign Up

Not able to get indexOf working in DataAction result

  • 1.  Not able to get indexOf working in DataAction result

    Posted 06-05-2025 18:45

    alex7 | 2023-12-21 11:27:10 UTC | #1

    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'


    alex7 | 2023-12-21 11:17:16 UTC | #2

    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

    677x148](upload://LA8INHrpbrjOrftrPfpJfnYWV3.png)

    ![image

    659x242](upload://xNmnyEAAyAteX6FyqIb3SRFYC8q.png)

    Data Action test


    alex7 | 2023-12-21 11:48:32 UTC | #3

    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.


    Richard.Schott | 2024-01-04 16:37:17 UTC | #4

    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 (https://github.com/json-path/JsonPath). 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.


    Richard.Schott | 2024-01-04 16:43:14 UTC | #5

    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.


    system | 2024-02-04 16:44:04 UTC | #6

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


    This post was migrated from the old Developer Forum.

    ref: 23806