Convert JsonNode object to Map

JavaJsonJacksonPlayframework 2.2

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionviscView Question on Stackoverflow
Solution 1 - JavamhogerheijdeView Answer on Stackoverflow