Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:


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:


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();

String age = node.get("age").asText();

String jsonText = node.asText();

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();

String name = node.get("name").toString();

String age = node.get("age").toString();

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.

As always, the code is available over on GitHub.

Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

res – Jackson (eBook) (cat=Jackson)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.