Karate Json Object - JsonPaths for nested array objects

Viewed 320

I am using the karate java-api to work with Json objects. I have the below json to be created:

{
    "a": [
        {
            "b": [
                {
                    "c": "hello"
                }
            ]
        }
    ]
}

When I try to json.set("$.a[0].b[0].c", "hello"), I get the below error :

Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List (java.util.LinkedHashMap and java.util.List are in module java.base of loader 'bootstrap')
    at com.intuit.karate.Json.createPath(Json.java:232)
    at com.intuit.karate.Json.createPath(Json.java:246)
    at com.intuit.karate.Json.setInternal(Json.java:199)
    at com.intuit.karate.Json.set(Json.java:148)

The json path syntax is correct as cross checked, any other point(s) which I might be missing?

Edit: I'm using v1.1.0 of karate-core

1 Answers

Looks like a bug in Karate where so much array nesting is not supported. To replicate:

* def foo = {}
* set foo.a[0].b[0].c = 'hello'
* print foo

Since you were keen to contribute to Karate, maybe you can fix this :) Feel free to open an issue.

Here's the workaround:

* def foo = {}
* set foo.a[0].b = [{ c: 'hello' }]
* print foo
Related