Search code examples
javajsondeserializationrest-assuredrest-assured-jsonpath

Deserialization using RestAssured but without following jsonpath


I have the following response body from a server:

{
  level1: {
    main1.abc: "A string",
    main1.def: "Another text",
    main2.abc: "Something else"
  }
}

and I'm using restAssured to process the response. I would like to get it as map with key representing the full string and their related values.

I've done it before however here the dot notation is automatically processed as jsonpath, so it gets deserialized as well, resulting in the following linked hash map:

level1 (key) > main1 (key) > abc (key) > "A string" (value)
             > main1 (key) > def (key) > "Another text" (value)
             > main2 (key) > abc (key) > "Something else" (value)

by calling

then().extract().body().jsonpath().getMap("");

I'm not able to find any specific settings in jsonPathConfig to control this. Am I looking at the wrong place?

thx!


Solution

  • I use Rest-Assured version 4.4.0, it works fine for you json.

    @Test
    void SO_69573815() {
        String text = "{\n" +
                "  \"level1\": {\n" +
                "    \"main1.abc\": \"A string\",\n" +
                "    \"main1.def\": \"Another text\",\n" +
                "    \"main2.abc\": \"Something else\"\n" +
                "  }\n" +
                "}";
        Map<String, Map<String, Object>> map = JsonPath.from(text).getMap("");
        System.out.println(map.get("level1").get("main1.abc"));
        //A string
    }