REST Assured 44 - Fetch Value From JSON Object Using JsonNode – Jackson – Get() & Path() Methods

REST Assured series summary REST Assured 44 - Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() methods

introduce

When we need to parse long and nested JSON, it is not convenient to create POJO classes. We need to use tree structure better.

prerequisite

Required Java Library

Because we use the Jackson API, make sure to import the Jackson Databind dependency package.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

Tree representation of JSON Object

JSON Object

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "married": false,
  "salary": 2000.54,
  "addressPin": 45324
}

Tree structure:
You can use one Online tools To browse the JSON above and present a tree structure:

Deserialize a JSON Object to Tree

We need to use the ObjectMapper class in Jackson API. The "readTree()" method provided by this class is responsible for deserializing a series of JsonNode instances in which the deserialize JSON content is expressed in a tree form.

We obtain the value through the get() and path() methods of the JsonNode class, and then convert it to the appropriate data type.

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
 
public class ParseJsonObjectToReadValues {
	
	@Test
	public void parseJsonObjectToReadValues() throws JsonMappingException, JsonProcessingException
	{
		String jsonObject = "{\r\n" + 
				"  \"firstName\": \"Amod\",\r\n" + 
				"  \"lastName\": \"Mahajan\",\r\n" + 
				"  \"married\": false,\r\n" + 
				"  \"salary\": 2000.54,\r\n" + 
				"  \"addressPin\": 45324\r\n" + 
				"}";
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get tree representation of json
		JsonNode jsonTree = objectMapper.readTree(jsonObject);
		// Get value of firstName as string
		String firstName = jsonTree.get("firstName").asText();
		String lastName = jsonTree.get("lastName").asText();
		// Get value of married as boolean
		boolean married = jsonTree.get("married").asBoolean();
		double salary = jsonTree.get("salary").asDouble();
		long addressPin = jsonTree.get("addressPin").asLong();
		
		System.out.println("FirstName is : "+firstName);
		System.out.println("LastName is  : "+lastName);
		System.out.println("Married is   : "+married);
		System.out.println("Salary is    : "+salary);
		System.out.println("Addresspin is: "+addressPin);
		
	}
 
}

Output:

FirstName is : Amod
LastName is  : Mahajan
Married is   : false
Salary is    : 2000.54
Addresspin is: 45324

Observe the method asText(), asBoolean() used above in order to parse into appropriate values. If you are not sure about the exact data type of the value, you can help with methods like isTextual(), isBoolean().

System.out.println(jsonTree.get("firstName").isTextual());
System.out.println(jsonTree.get("lastName").isTextual());
System.out.println(jsonTree.get("married").isBoolean());
System.out.println(jsonTree.get("salary").isDouble());
System.out.println(jsonTree.get("addressPin").isLong());

If the value is parsed into the wrong data type, an exception will not be thrown. For example, use the asBoolean() method to parse a String into a boolean. According to the official Jackson API Java document, the following situations will occur:

The asBoolean() method converts the value of this node into boolean. The mapping relationship of JSON boolean is that integer non-zero corresponds to true and 0 corresponds to false; String 'true' and 'false' correspond to boolean true and false respectively. If the object cannot be converted to a boolean value (including Arrays and Objects), it will return false by default and will not throw exceptions.

Similar to asLong(), this method converts the node value to a Java long type. The number adopts the mandatory conversion rules, the Boolean value false is converted to 0, and the true is 1. The string type is parsed according to the default Java language integer rules. If the object cannot be converted to a loing value (including Arrays and Objects), a 0 will be returned by default and no exception will be thrown.

Retrieving value for a non-existing node

When you use the get() method to get the value of a nonexistent node, you will get a NullPointerException exception.

// Retrieving value of non-existing key
System.out.println(jsonTree.get("nonExistingNode").asText());

You can use these override methods as text (string DefaultValue) and as int (int DefaultValue) to handle unexpected null values. However, to use these methods, you must use the path() method instead of the get() method. Because using get() above will get a NullPointerException exception.

According to the official document, the path() method is similar to the get(String) method, except that if the node does not exist, it will not return null (because the node is not an object or an object without specific fields), a "missing node" will be returned, so it is safer to use the path() method.

// Retrieving value of non-existing key
		String s = jsonTree.get("nonExistingNode").asText();
		System.out.println(s);

The above code will output a NullPointerException exception.

// Retrieving value of non-existing key using path
		String s1 = jsonTree.path("nonExistingNode").asText();
		System.out.println(s1);

The above code will output "" empty string

Keywords: JSON jackson API testing

Added by mikesmith76 on Sun, 19 Sep 2021 12:00:07 +0300