Skip to content

non-numeric value used as array index error when nested array predicate is first #205

Description

@schmurgon

Using the following as input

{
  "Email": [
    {
      "type": "work",
      "address": [{ "email":"fred.smith@my-work.com"}, {"email":"fsmith@my-work.com"}]
    },
    {
      "type": "home",
      "address": [{"email":"freddy@my-social.com"}, {"email":"frederic.smith@very-serious.com"}]
    }
  ]
}

I'm attempting to select and replace the element that matches the filter predicate:

(
    $emailElement := Email[    
       address[ email="fred.smith@my-work.com" ]
    ][0];

    $selectedEmailElements := {        
        "Email": [$emailElement]
    };
 
    $merge([$, $selectedEmailElements])
)

This works perfectly in the node version and on the demo https://try.jsonata.org/ but results in an exception on jsonata4java 1.7.9

com.api.jsonata4java.expressions.EvaluateException: non-numeric value used as array index
at com.api.jsonata4java.expressions.Expressions.evaluate(Expressions.java:195)
Caused by: com.api.jsonata4java.expressions.NonNumericArrayIndexException: non-numeric value used as array index
at com.api.jsonata4java.expressions.ExpressionsVisitor.visitArray(ExpressionsVisitor.java:1018)
at com.api.jsonata4java.expressions.ExpressionsVisitor.visitArray(ExpressionsVisitor.java:99)
at com.api.jsonata4java.expressions.generated.MappingExpressionParser$ArrayContext.accept(MappingExpressionParser.java:521)

When the expression is changed to include a meaningless predicate prior to the array filter then the correct result is returned as expected:

(
    $emailElement := Email[    
      "1" = "1" and /* HACK */
       address[ email="fred.smith@my-work.com" ]
    ][0];

    $selectedEmailElements := {        
        "Email": [$emailElement]
    };
 
    $merge([$, $selectedEmailElements])
)

Result

{
  "Email" : [ {
    "type" : "work",
    "address" : [ { "email" : "fred.smith@my-work.com" }, { "email" : "fsmith@my-work.com" } ]
  } ]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions