Posted In: JSON

Check two JSON Objects for equality – Jackson

 

This example uses jackson library

 

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.8.6</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.8.6</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.8.6</version>
</dependency>

 

Examples using Hamcrest-JSON

 

1. Success test with a JSON string

	@Test
	public void testJson3() {
		try {
			JsonNode expectedNode = new ObjectMapper().readTree(expectedData3());
			JsonNode actualNode = new ObjectMapper().readTree(actualData3());
			assertEquals(expectedNode, actualNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String expectedData3() {
		String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

private String actualData3() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

2. Error test with a JSON string

@Test
public void testJson4() {
	try {
		JsonNode expectedNode = new ObjectMapper().readTree(expectedData4());
		JsonNode actualNode = new ObjectMapper().readTree(actualData4());
		assertEquals(expectedNode, actualNode);
	} catch (JsonProcessingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

private String expectedData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"20000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

private String actualData4() {
	String json = "{" + "	\"portfolio\": [" 
			+ "		{"
	        + "			\"stock\": \"Infosys1\","
	        + "			\"quantity\": \"100\","
	        + "			\"buy\": \"1011\","
	        + "			\"profit\": \"10000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"State Bank of India\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"204\","
	        + "			\"profit\": \"200000\"" 
	        + "		},"
	        + "		{"
	        + "			\"stock\": \"Reliance Industries\","
	        + "			\"quantity\": \"1000\","
	        + "			\"buy\": \"1200\","
	        + "			\"profit\": \"12000\"" 
	        + "		}"
	        + "	]" + "}";
	return json;
}

 

3. Success test with a JSON String

This test covers How do I compare 2 json files having same key-values but in different order in java?
 

@Test
public void testJson5() {
	try {
		JsonNode expectedNode = new ObjectMapper().readTree(expectedData5());
		JsonNode actualNode = new ObjectMapper().readTree(actualData5());
		assertEquals(expectedNode, actualNode);
	} catch (JsonProcessingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

private String expectedData5() {
	String json = "{" +
			"  \"portfolio1\":" + 
			"    {" +
			"      \"stock\": \"Infosys\"," +
			"      \"quantity\": \"100\"," +
			"      \"buy\": \"1011\"," +
			"      \"profit\": \"10000\"" +
			"    }," +
			"  \"portfolio2\":" + 
			"    {" +
			"      \"stock\": \"SBI\"," +
			"      \"quantity\": \"1000\"," +
			"      \"buy\": \"211\"," +
			"      \"profit\": \"20000\"" +
			"    }" +
			"}";
	return json;
}

private String actualData5() {
	String json = "{" +
			"  \"portfolio2\":" + 
			"    {" +
			"      \"stock\": \"SBI\"," +
			"      \"quantity\": \"1000\"," +
			"      \"buy\": \"211\"," +
			"      \"profit\": \"20000\"" +
			"    }," +
			"  \"portfolio1\":" + 
			"    {" +
			"      \"stock\": \"Infosys\"," +
			"      \"quantity\": \"100\"," +
			"      \"buy\": \"1011\"," +
			"      \"profit\": \"10000\"" +
			"    }" +
			"}";
		return json;
}

 

Tags: , ,

by , on February 24th, 2017

  • Categories