camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erj2code <>
Subject How do I marshall nested key,value pairs into JSON with Camel and Jackson library?
Date Tue, 15 Oct 2013 19:47:52 GMT
I have a Java project that currently returns a map to Camel without any
nested key,value pairs, and the Jackson library marshalls it just fine into
JSON using the Jackson library.

For example if I put the following two key,values into a demoMap:

    Map<String,String> demoMap = new TreeMap<String,String>

I get the following JSON:


However, now some of my key,value entries will have an optional visibility
that I need to put as a nested key value in my map.  Any key,values that
don't have an optional visibility will use the default.  So, for example if
I specify visibilities for key1, and key2, but not key3 I want to get JSON
out that looks like this:

     "visibility" : { "key1": "a&b&!c", "key2": "a&b", "_default": "a" }

How can I get Camel to marshall a Java object with nested key,value pairs? 
I'm a visual learner, so a simple example would be helpful.

I tried changing my Map to have a value as an object i.e.,:

    Map<String,Object> demoMap = new TreeMap<String,Object>

and then tried adding nested key,values for some keys with an ArrayList
for reference, but realized that this just gives me a bunch of nested values
under a key, not a bunch of nested key,value pairs.

Even when I tried it for grins, I got an error from the Camel processor with
a java.lang.ClassCastException stating java.util.ArrayList cannot be cast to

And similarly when I tried to nest a Map inside my demoMap I got this

    3244 [] ERROR
org.apache.camel.processor.DefaultErrorHandler  - Failed delivery for
exchangeId: e6518e39-89b7-435e-96d9-ce26811ac67e. Exhausted after delivery
attempt: 1 caught: java.lang.ClassCastException: java.util.HashMap cannot be
cast to java.lang.String

So I know how NOT to do it. :-/

I re-read the Camel JSON documentation at
but as of this writing it doesn't specify an example with nested key,value

View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message