Suppose the GET request returns me some JSON object:
{
"a": 1, "b": 2, "c": 3, "date": "current_date"
}
And I have in my hand a similar object, which I'd like to check it's identical for the the keys "a", "b", "c" and ignore the "date" key.
How can I do that?
I have been using JsonUnit and it really helps
String json1 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"30-07-2020\"\r\n" + "}";
String json2 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"31-07-2020\"\r\n" + "}";
assertThatJson(json1).whenIgnoringPaths("date").isEqualTo(json2);
Static Import :
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
Dependency :
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.18.1</version>
<scope>test</scope>
</dependency>
You will have to add json.org
or Jackson 1.x
or Jackson 2.x
or Johnzon
or Gson
to the classpath as well
I use Jackson