title: Does camel-jackson 2.12.1 support marshalling of nested objects into nested JSON?
body: <p>I have the following two Java classes (SensorData, and Visibility) to store dummy sensor data, and their respective visibilities into two objects: </p>
  
  <h1>SensorData.java</h1>
  
  <pre><code>package com.data.world2; 
  
  import java.util.Map; 
  import java.util.TreeMap; 
  
  import com.fasterxml.jackson.annotation.JsonAnyGetter; 
  import com.fasterxml.jackson.annotation.JsonAnySetter; 
  import com.fasterxml.jackson.annotation.JsonProperty; 
  
  public class SensorData { 
  
      private final Map&lt;String, Object&gt; keyvalues = new TreeMap&lt;String, Object&gt;(); 
  
      @JsonProperty 
      private Visibility visibility; 
  
      // getters and setters 
      @JsonAnyGetter 
      public Map&lt;String, Object&gt; getKeyvalues() { 
        return keyvalues; 
      } 
  
      @JsonAnySetter 
      public void setKeyvalues(final String key, final Object value) { 
        this.keyvalues.put(key, value); 
      } 
  
      // getters and setters 
      public Visibility getVisibility() { 
         return visibility; 
      } 
  
      public void setVisibility(Visibility visibility) { 
         this.visibility = visibility; 
      } 
  
  } 
  </code></pre>
  
  <h1>Visibility.java</h1>
  
  <pre><code>package com.data.world2; 
  
  import java.util.Map; 
  import java.util.TreeMap; 
  
  import com.fasterxml.jackson.annotation.JsonAnyGetter; 
  import com.fasterxml.jackson.annotation.JsonAnySetter; 
  
  public class Visibility { 
  
      private final Map&lt;String, Object&gt; keyvalues = new TreeMap&lt;String, Object&gt;(); 
  
      // getters and setters 
  
      @JsonAnyGetter 
      public Map&lt;String, Object&gt; getKeyvalues() { 
         return keyvalues; 
      } 
  
      @JsonAnySetter 
      public void setKeyvalues(final String key, final Object value) { 
         this.keyvalues.put(key, value); 
      } 
  
  } 
  </code></pre>
  
  <p>And in my SensorGenerator class I store SensorData and Visibility keyvalue objects.  For example, suppose I have two keyvalue pairs from a sensor (make and model), and only the "make" keyvalue pair has a visibility specified; the "model" keyvalue pair uses the default visibility: </p>
  
  <pre><code>    // create a sensorData object, and a visibility object 
      SensorData sensorData = new SensorData(); 
      Visibility visibility = new Visibility(); 
  
      sensorData.setKeyValues("s1make", "Apple"); // set sensor 1 make 
      visibility.setKeyValues("s1make", "public"); // set sensor 1 make visibility 
      sensorData.setKeyValues("s1model", "iPhone5"); // set sensor 1 model 
      // sensor 1 model visibility not specified 
  
      // set default visibility 
      visibility.setKeyValues("_default", "private"); // set default visibility for sensor keyvalue pairs 
  </code></pre>
  
  <p>Then I added the Visibility object to the SensorData object to get nested visibilities: </p>
  
  <pre><code>    // add the visibility to the SensorData object 
      sensorData.setVisibility(visibility); 
  </code></pre>
  
  <h1>UPDATED:</h1>
  
  <p>The issue was that I was not returning the list back to Camel correctly for marshalling to JSON.</p>
  
  <p>When I changed my code to the following I had the full nested JSON marshalled:</p>
  
  <pre><code> List&lt;SensorData&gt; sensorDataList = new ArrayList&lt;SensorData&gt;();
   sensorDataList.add(sensorData);
  </code></pre>
  
  <p>The following is the previous way I had which was incorrect (just here for historical info)</p>
  
  <p>And then I return a List of SensorData objects to my Camel thread for marshalling to JSON with the Jackson library (camel-jackson): </p>
  
  <pre><code>    // Build a sensorDataList based on the keyvalues stored in the SensorData object 
      List&lt;SensorData&gt; sensorDataList = new ArrayList(sensorData.getKeyvalues().entrySet()); 
  </code></pre>
  
  <p>When I run my Java program I'd expect to see the following nested JSON from my Camel route: </p>
  
  <pre><code>[{"key":"s1make","value":Apple"}, 
   {"key":"s1model","value":"iPhone5"}, 
   {"visibility": {"key":"s1make","value":"public", 
                   "key":"_default","value":"private"} 
  }] 
  </code></pre>
  
  <p>But instead, I only see are the SensorData keyvalues marshalled into JSON i.e.,: </p>
  
  <pre><code>[{"key":"s1make","value":Apple"}, 
   {"key":"s1model","value":"iPhone5"}] 
  </code></pre>
  
  <p>How come I don't see the "s1make" and "_default" visibilities as nested JSON?  Does camel-jackson 2.12.1 not support marshalling of nested objects into nested JSON? </p>
  
  <p>Btw, here is a snippet of my applicationContext.xml where I specify the marshalling to JSON: </p>
  
  <p></p>
  
  <pre><code>      &lt;camel:dataFormats&gt;
           &lt;camel:json id="jack" library="Jackson"/&gt;
        &lt;/camel:dataFormats&gt;
  
        &lt;camel:route&gt;
  
               &lt;camel:from 
                       uri="timer://hello.world.request.timer?fixedRate=true&amp;amp;period={{config.timeout}}" /&gt;
               &lt;camel:to uri="log:hello.world.request?level=INFO&amp;amp;showAll=true" /&gt;
               &lt;camel:bean ref="helloWorld" /&gt;
  
  
               &lt;camel:marshal ref ="jack"/&gt;
               &lt;camel:convertBodyTo type="java.lang.String" /&gt;
               &lt;camel:log message="${body}"/&gt;  
  
  
               &lt;camel:log message="printing values read from config.properties file"/&gt;
               &lt;camel:log message="config.timeout= {{config.timeout}}"/&gt;  
               &lt;camel:log message="config.numSamples= {{config.numSamples}}"/&gt;
               &lt;camel:log message="config.defaultViz= {{config.defaultViz}}"/&gt;
  
  
               &lt;camel:to uri="log:hello.world.response?level=INFO&amp;amp;showAll=true" /&gt;
  
         &lt;/camel:route&gt;
  
   &lt;/camel:camelContext&gt;
  </code></pre>
  
  <p>Last week I posted a similar question on SO, but realized after I posted it that I had to change my implementation to return a list to Camel instead of a map. 
  <a href="https://stackoverflow.com/questions/19389696/how-do-i-marshall-nested-key-value-pairs-into-json-with-camel-and-jackson-librar">How do I marshall nested key,value pairs into JSON with Camel and Jackson library?</a></p>