Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll explore the difference between asText() and toString() in Jackson’s JsonNode.

The JsonNode class enables parsing and manipulating JSON data. The two common methods of JsonNode when interacting with String data are asText()  and toString(). They may seem similar at first, but they have important differences.

2. Dependencies

Let’s first add the jackson-databind dependency to the pom.xml:

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

3. Using asText()

In this article, we’ll use the Jackson ObjectMapper object and the Jackson JsonNode object.

The asText() method returns the text value of a JsonNode as a String, but it works differently depending on the type of the JsonNode:

  • For TextNode, it would return the text value of the node
  • For numeric nodes, it returns the numeric value of the node as a String
  • For BooleanNode nodes, it returns true or false
  • For ArrayNode and ObjectNode, it returns an empty String

The idea of asText() is to return the node’s value without manipulations. Since asText() is an abstract method of JsonNode, all nodes have an implementation where each returns its own value.

It’ll return an empty String if that’s impossible without manipulating the node (for example, due to sub-nodes):

String json = "{\"name\":\"John\",\"age\":30}";
JsonNode node = new ObjectMapper().readTree(json);

String name = node.get("name").asText();
assertThat(name).isEqualTo("John");

String age = node.get("age").asText();
assertThat(age).isEqualTo("30");

String jsonText = node.asText();
assertThat(jsonText).isEmpty();

4. Using toString()

The toString() method is overridden from the Object and returns a String representation of the JsonNode‘s data. This means that if we perform this operation, it’ll return the JSON’s text representation (including child nodes in the case of ObjectNode and ArrayNode) with quotation marks and escaping characters.

This method can be useful for debugging or generating JSON text that can be written to files, APIs, and more.

Unlike asText(), if we apply toString() on a TextNode, it’ll return a String with quotation marks and escape characters:

String json = "{\"name\":\"John\",\"age\":30}";
JsonNode node = new ObjectMapper().readTree(json);

String jsonString = node.toString();
assertThat(jsonString).isEqualTo("{\"name\":\"John\",\"age\":30}");

String name = node.get("name").toString();
assertThat(name).isEqualTo("\"John\"");

String age = node.get("age").toString();
assertThat(age).isEqualTo("30");

5. Special Characters

Another aspect to consider is how both methods handle special and Unicode characters. asText() automatically escapes any special characters in the text, such as double quotes and backslashes, to ensure that the resulting String is valid JSON.

An example to demonstrate how asText() correctly unescapes the double quotes in the text value, while toString() does not automatically escape special characters. If we have special characters in our JSON data, using toString() could result in invalid JSON text that cannot be parsed correctly:

String specialCharsJson = "{\"text\":\"Hello \\\"world\\\" !\"}";
JsonNode specialCharsNode = new ObjectMapper().readTree(specialCharsJson);
    
String specialCharsJsonAsText = specialCharsNode.get("text").asText();
assertThat(specialCharsJsonAsText).isEqualTo("Hello \"world\" !");

String specialCharsJsonToString = specialCharsNode.get("text").toString();
assertThat(specialCharsJsonToString).isEqualTo("\"Hello \\\"world\\\" !\"");

6. Conclusion

In this article, we discussed asText() and toString() methods for working with JSON data in Java.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW