Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JsonNode object to Map

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

like image 849
visc Avatar asked Nov 05 '14 20:11

visc


1 Answers

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>>(){});
like image 108
mhogerheijde Avatar answered Nov 19 '22 12:11

mhogerheijde