Interface JsonNode

All Known Subinterfaces:
JsonArrayNode, JsonBooleanNode, JsonNullNode, JsonNumberNode, JsonObjectNode, JsonStringNode

public interface JsonNode

This interface is used to represent a JSON node, and acts as the base for all other JsonNode types (see subinterfaces).

You can create a JsonNode from a raw JSON string using jsonNode(String), which will attempt to parse the JSON into a specific JsonNode type.

To retrieve the created JsonNode as a JsonArray, or a JsonObject, call asArray() or asObject() respectively. This provides additional utility for the specific JSON type.

Note: If you are parsing a JsonObject, you can use JsonObjectNode and its utility methods. Call asObject() on your JsonNode.

Note: Attempting to retrieve the JsonNode as the wrong type will throw an IllegalStateException.

     JsonNode jsonNode = JsonNode.create("[]");       // The string parsed, and the underlying type will be JsonArrayNode.
     JsonObjectNode objectNode = jsonNode.asObject(); // Throws IllegalStateException, as the JSON string was parsed as a JsonArrayNode.
     JsonArrayNode arrayNode = jsonNode.asArray();    // Successful
 

Each specific JsonNode type has a corresponding factory method:

     // Create a JsonStringNode with the value "foo"
     JsonStringNode stringNode = JsonStringNode.jsonStringNode("foo");

     // Create a JsonNumberNode with the value 2.5
     JsonNumberNode numberNode = JsonNumberNode.jsonNumberNode(2.5);

     // Create a JsonArrayNode containing the two above nodes
     JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
         stringNode,
         numberNode
     );
 

Any JsonNode can return its JSON string representation:

     JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
            stringNode,
            numberNode
     );

     String arrayNodeAsJson = arrayNode.toJsonString();

     System.out.println(arrayNodeAsJson);
 

Prints:

     [
       "foo",
       2.5
     ]