Search code examples
restrest-assured

How do I get the value of a key if it contains a "/" in Rest Assured / java?


Example json is
{
"model": {
"sub-model": {
"/example/1":"alaska",
"value": "Consistently"
}
}
}

I tried to fetch the value of the key -"/example/1" with the below code and I am getting java.lang.IllegalArgumentException: The parameter "1" was used but not defined. Define parameters using the JsonPath.params(...) function

String keyVal=given()
                .when()
                .get(url)
                .then().extract().path("model.sub-model./example/1");

Solution

  • In your case, you just have to put your /example/1 in " like this:

    String keyVal=given()
                    .when()
                    .get(url)
                    .then().extract().path("model.sub-model.\"/example/1\"");
    

    I also added backslash as it's special escape character in Java. Adding the " alone would cause termination of the String.