Jackson – Marshall String to JsonNode

1. Overview

This quick tutorial will show how to use Jackson 2 to convert a JSON String to a JsonNode (com.fasterxml.jackson.databind.JsonNode).

If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial.

2. Quick Parsing

Very simply, to parse the JSON String we only need an ObjectMapper:

@Test
public void whenParsingJsonStringIntoJsonNode_thenCorrect() 
  throws JsonParseException, IOException {
    String jsonString = "{"k1":"v1","k2":"v2"}";

    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(jsonString);

    assertNotNull(actualObj);
}

3. Low Level Parsing

If, for some reason, you need to go lower level than that, the following example exposes the JsonParser responsible with the actual parsing of the String:

@Test
public void givenUsingLowLevelApi_whenParsingJsonStringIntoJsonNode_thenCorrect() 
  throws JsonParseException, IOException {
    String jsonString = "{"k1":"v1","k2":"v2"}";

    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(jsonString);
    JsonNode actualObj = mapper.readTree(parser);

    assertNotNull(actualObj);
}

4. Using the JsonNode

After the JSON is parsed into a JsonNode Object, we can work with the Jackson JSON Tree Model:

@Test
public void givenTheJsonNode_whenRetrievingDataFromId_thenCorrect() 
  throws JsonParseException, IOException {
    String jsonString = "{"k1":"v1","k2":"v2"}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(jsonString);

    // When
    JsonNode jsonNode1 = actualObj.get("k1");
    assertThat(jsonNode1.textValue(), equalTo("v1"));
}

5. Conclusion

This article illustrated how to parse JSON Strings into the Jackson JsonNode model to enable a structured processing of the JSON Object.

The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

Related posts:

Python String endswith()
“Stream has already been operated upon or closed” Exception in Java
Introduction to the Java NIO2 File API
Java 9 Stream API Improvements
Java Program to Check whether Graph is a Bipartite using DFS
Python Program to Remove Punctuations From a String
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Spring Security Registration – Resend Verification Email
Spring Data – CrudRepository save() Method
Spring Security Remember Me
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Implement Ternary Search Algorithm
Lớp Collectors trong Java 8
Introduction to the Java ArrayDeque
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Find kth Largest Element in a Sequence
Working With Maps Using Streams
Java Program to Represent Graph Using Adjacency List
Mapping Nested Values with Jackson
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Perform Polygon Containment Test
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Search for an Element in a Binary Search Tree
Hướng dẫn Java Design Pattern – Abstract Factory
Giới thiệu Design Patterns
Spring Security 5 for Reactive Applications
HttpClient Timeout
Weak References in Java
Java Program to Solve a Matching Problem for a Given Specific Case
Circular Dependencies in Spring