简体   繁体   中英

Unable to extract the data from the Nested JSON path

I am new to API automation. Here is my JSON structure. I want to extract the "LunId" from all the 3 arrays. Path is mentioned in the code.

JSON结构1
JSON扩展结构1

Error1:

在代码中使用“ $”时

Error2:

在代码中不使用“ $”时

@Test

public void getAllLuns(){   

Response resp = given().header("Authorization", "Bearer "+"ddnsdndnn22x");
                when().
                get("http://10.100.242.11/api/v1/luns");

String Name =  resp.                
               then().
               contentType(ContentType.JSON).
               extract().
               path("$..LunId").toString();

System.out.println("Lun Name is--->"+LunId);

I am extracting the correct data in JSON path Extractor online, but while running it in eclipse it gives me an error. Problem is in the path. Please help, how to work with these kinds of paths. Getting Error1- when using"$" in the path and Error2 when not using it.

This can easily be done using JSONPath Lib

https://github.com/json-path/JsonPath

   public List<String> getLunIDNames() {

    return lunIDNamess = JsonPath.read("ValidJSON", "$..LunId[*].Name");

}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM