Deserializing JSON array of objects into POJO

1,522 views
Skip to first unread message

Alex

unread,
Feb 14, 2017, 9:48:57 AM2/14/17
to REST assured
Hi,

I have the following JSON response for one of my APIs:
{
   
"data": [
       
{
           
"encodedKey": "12345",
           
"id": "77",
           
"creationDate": "2017-01-23T13:02:56Z",
           
"lastModifiedDate": "2017-01-25T15:54:59Z",
           
"name": "Branch 1",
           
"phoneNumber": "",
           
"emailAddress": ""
       
}
   
]
}

and the following object model:
public class Branch {

   
@NotNull
   
private String id;

   
@NotNull
   
private String name;

   
@NotNull
   
@Past
   
private Date creationDate;

   
@NotNull
   
private Date lastModifiedDate;

   
private String phoneNumber;

   
@Email
   
private String emailAddress;

   
// getters & setters
}

My question is if there's a possible solution to serializing/deserializing other than wrapping the Branch into another class (e.g. "BranchWrapper") containing a List of Branches. This would be tedious to do for all my objects.

If would be very handy to be able to do something like below:

Branch branchBasic = given().pathParam("id", branchId)
               
.when()
               
.get("/branches/{id}")
               
.then()
               
.rootPath("data[0]") // would extract the Response starting with the given root path
               
.extract()
               
.as(Branch.class);

Any alternatives?


Thanks,

Johan Haleby

unread,
Feb 15, 2017, 4:09:21 AM2/15/17
to rest-a...@googlegroups.com
You should be able to do something like this:

List<Branch> branches = given().pathParam("id", branchId)
                .when()
                .get("/branches/{id}")
                .then()                
                .extract()
                .jsonPath()
                .getList("data", Branch.class);

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex

unread,
Feb 15, 2017, 4:14:46 AM2/15/17
to REST assured
Thanks Johan! Found the exact same solution in the meanwhile.

Alex
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages