-1

How to deserialize below JSON?

When i am using below code Response response=engine.getResponse(Method.GET,strURL,strBAUsername,strBAPassword,null,ContentType.JSON); userEdit=response.as(TestUserRoleInfoList.class); // TestUserRoleInfoList is a POJO class

@JsonIgnoreProperties(ignoreUnknown=true) public class TestUserRoleInfoList {

@JsonProperty("userRoleInfoList")
List<UserRoleInfo> userRoleInfo=new ArrayList<UserRoleInfo>();

public List<UserRoleInfo> getUserRoleInfo() {
    return userRoleInfo;
}

public void setUserRoleInfo(List<UserRoleInfo> userRoleInfo) {
    this.userRoleInfo = userRoleInfo;
}

}

Somehow it unable to parse Regions internal class. But i am least bothered. I need collectionRole fields.

How can i get the list of collection roles?

{
    "id": 5595,
    "userName": "abc",
    "firstName": "abc",
    "lastName": "ijk",
    "additionalName": "Ernest",
    "namePrefix": {
        "id": null,
        "prefix": null,
        "createdAt": null,
        "updatedAt": null
    },
    "nameSuffix": {
        "id": null,
        "suffix": null,
        "createdAt": null,
        "updatedAt": null
    },
    "emplId": "11111",
    "workDayId": "11111",
    "staffEmailAddress": null,
    "facultyEmailAddress": "abc.xyz@xxxx.edu",
    "userRoleInfoList": [
        {
            "userId": 5595,
            "collectionRole": "program-chair",
            "regions": [
                {
                    "id": 1,
                    "region": "Germany 1",
                    "regionalDirectorFirstName": "aaaa",
                    "regionalDirectorLastName": "bbbb",
                    "associateDirectorFirstName": null,
                    "associateDirectorLastName": null,
                    "division": {
                        "id": 2,
                        "name": "Europe",
                        "deletedFlag": false,
                        "createdAt": null,
                        "updatedAt": null
                    },
                    "deletedFlag": false,
                    "createdAt": null,
                    "updatedAt": null
                }
            ],
            "school": {
                "id": 3,
                "name": "Arts and Sciences",
                "dean": null,
                "createdAt": null,
                "updatedAt": null
            },
            "facultyPhoto": null,
            "primary": false
        },
        {
            "userId": 5595,
            "collectionRole": "faculty",
            "regions": [
                {
                    "id": 3,
                    "region": "Germany 1",
                    "regionalDirectorFirstName": "aaaa",
                    "regionalDirectorLastName": "bbbb",
                    "associateDirectorFirstName": null,
                    "associateDirectorLastName": null,
                    "division": {
                        "id": 2,
                        "name": "Europe",
                        "deletedFlag": false,
                        "createdAt": null,
                        "updatedAt": null
                    },
                    "deletedFlag": false,
                    "createdAt": null,
                    "updatedAt": null
                }
            ],
            "school": {
                "id": 3,
                "name": "Arts and Sciences",
                "dean": null,
                "createdAt": null,
                "updatedAt": null
            },
            "facultyPhoto": null,
            "primary": true
        }
    ]
}
Uday
  • 1,433
  • 10
  • 36
  • 57

1 Answers1

0

You can use org.json.JSONObject

    Response response = when()
            .get("api_url")
            .then()
            .extract().response();

    JSONObject jsonObject = new JSONObject(response.getBody().asString());
    JSONArray jsonArray = jsonObject.getJSONArray("userRoleInfoList");
    List<String> stringList = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        stringList.add(jsonArray.getJSONObject(i).getString("collectionRole"));
    }
    System.out.println(stringList);

This will output [program-chair, faculty]

kaweesha
  • 803
  • 6
  • 16