Simplified way to ignore all data values when comparing two JSon responses

2,268 views
Skip to first unread message

mdpete...@comcast.net

unread,
May 21, 2013, 10:16:29 AM5/21/13
to rest-a...@googlegroups.com
I'd like to compare the Json structure of two responses while ignoring data differences between the two. I was wondering what the simplest way to achieve that would be. 

Thanks,
Mike

Johan Haleby

unread,
May 21, 2013, 10:20:23 AM5/21/13
to rest-a...@googlegroups.com
Do you mean like a diff tool?


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mdpete...@comcast.net

unread,
May 21, 2013, 12:22:27 PM5/21/13
to rest-a...@googlegroups.com
Hi Johan.  I mean, can I get just the path of the elements  in a Json tree. That way I could make sure the trees are the same even if data is different. I want just the element names. I want to do something like this but without going outside rest-assured and JsonPath:

Johan Haleby

unread,
May 22, 2013, 2:24:33 AM5/22/13
to rest-a...@googlegroups.com
Hi,

If I understand you correctly you can do something like this:

expect().
            body("x.y.z", not(nullValue())).
            body("x.y.z2", not(nullValue())).
when().
           get("/something");

If you already have a JSON document you could using JsonPath:

Object value = from(json).get("x.y.z");
assertThat(value).isNotNull();

Regards,
/Johan

mdpete...@comcast.net

unread,
May 22, 2013, 8:11:58 AM5/22/13
to rest-a...@googlegroups.com
Hi. My real goal is to compare the trees of the two objects while not considering what values each node has. The reason for this is that I'd like to have a boolean in my test that when True will generate a file from the response. Then I will change it to False and compare the names of each path node in my actual response to those in my saved file. I don't want to bring the values (the data) of the [ath nodes into the picture into the picture. My goal is to check that the structure of the JSon is the same in both. When I find that they don't match any more, I can determine if there's a bug or a deliberate code change that changed the Json structure. If the change is not a bug, I will regenerate the file to get a new baseline response. I'm hoping this type of test will save time on test maintenance and make things less brittle. It is similar to the type of thing I want to do in my other post where I'm comparing a baseline file to a response but in this case, I don't want to consider the data in my comparison of the response and the file.

mdpete...@comcast.net

unread,
May 22, 2013, 12:37:07 PM5/22/13
to rest-a...@googlegroups.com
Maybe a simpler way of saying this is that I want to compare just the keys of each key:value pair in the Json responses. I'd like a simple way to get jsut the hierarchy of the keys for the entire response.

Johan Haleby

unread,
May 23, 2013, 2:23:20 AM5/23/13
to rest-a...@googlegroups.com
Ok I think I get it. Unfortunately there's currently no good way to achieve this with RA or JsonPath.

Regards,
/Johan

mdpete...@comcast.net

unread,
May 23, 2013, 10:37:56 AM5/23/13
to rest-a...@googlegroups.com
Thanks. I just found a way to do this. Haven't done much with it yet, but seems to do what I want. 
I'm using the org.json.JSONObject and JSONArray classes. The JSONObject class has a names() method that returns the list of key names for a JSONObject. Here's the code I'm using to get the key names of of the Json objects in an array in my response:

JSONObject resObj= new JSONObject(res);
JSONArray jsData = jsObj.getJSONArray("data");
JSONObject dataObj = (JSONObject) jsData.get(0);
JSONArray fields = dataObj.names();

Johan Haleby

unread,
May 23, 2013, 2:16:54 PM5/23/13
to rest-a...@googlegroups.com
Thanks for sharing, glad you found a way to solve it.
Reply all
Reply to author
Forward
0 new messages