---
title: "Examples for working with JSON in Play Framework 2 with Java"
date: 2014-03-18T20:32:35+00:00
author: "poornerd"
tags: ["json"]
canonical: https://www.poornerd.com/2014/03/18/examples-for-working-with-json-in-play-framework-2-with-java/
source: Raw Markdown twin of the HTML article; content is the original source.
---
* String to JSON:  <pre class="brush: plain; title: ; notranslate" title="">JsonNode json = mapper.readTree(notification.getSharedData());</pre>

  * Json to Object:  <pre class="brush: plain; title: ; notranslate" title="">Asset asset = mapper.readValue(a.toString(), Asset.class)</pre>

  * Json to extisting Object:  <pre class="brush: plain; title: ; notranslate" title="">asset = mapper.readerForUpdating(asset).readValue(assetJson.toString())</pre>

  * Object to JsonString: <pre class="brush: plain; title: ; notranslate" title="">mapper.writeValueAsString(new ResultDocument(document))</pre>

  * Convert Object ot JsonNode: <pre class="brush: plain; title: ; notranslate" title="">JsonNode documentNode = mapper.convertValue(document, JsonNode.class);</pre>
    
    This causes an error &#8211; `Caused by: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle` so do this workaround: 
    
    <pre class="brush: plain; title: ; notranslate" title="">String messageJson = null;

String documentAsString = BaseApiController.mapper.writeValueAsString(document);

JsonNode documentNode = BaseApiController.mapper.readTree(documentAsString);</pre>
