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:

Java Program to Implement Counting Sort
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
An Intro to Spring Cloud Contract
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java String to InputStream
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Check whether Undirected Graph is Connected using DFS
Hướng dẫn Java Design Pattern – Interpreter
Guava CharMatcher
Reactive WebSockets with Spring 5
Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Introduction to PCollections
Java Program to Check if a Matrix is Invertible
Optional trong Java 8
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Multi-line String
Converting a List to String in Java
Receive email by java client
Guide to System.gc()
Spring Boot - Tomcat Port Number
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java Program to Find the Longest Path in a DAG
Guide to the Volatile Keyword in Java
How to Read HTTP Headers in Spring REST Controllers
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Word Wrap Problem
Apache Commons Collections Bag
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Generate Random Numbers Using Middle Square Method
How to Convert List to Map in Java