Convert JsonNode object to Map

visc picture visc · Nov 5, 2014 · Viewed 68.1k times · Source

I have a C# program that sends me a json object. I'm making a Java Play website to capture the POST data. I get the correct data as a JsonNode object but need to convert it into a Map.

I'm using com.fasterxml.jackson.databind.JsonNode

Here is where I correctly capture the JsonNode object:

public static Result index() {
    JsonNode json = request().body().asJson();
}

Now that I have the object I need to figure out how to convert it into a Map so that I can so some magic on it. Later I'll want to convert the Map back into a json object to be sent in the response.

I've been looking in the documentation, but the methods available don't scream as the solution.

Here is the documentation I've been referencing for this particular JsonNode object: http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/JsonNode.html

Answer

mhogerheijde picture mhogerheijde · Jan 27, 2015

Got here trying to find the answer myself. Dug a little deeper and found a bit the answer here

Basically just use the ObjectMapper to convert the value for you:

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> result = mapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>(){});