Take the 2-minute tour ×

I am using Mule. I have a JAVA Object that is populated from my internal Class..It is actually a HashMap<String,Object>. Object can be anything..another HashMap, OR List etc ..Now i have to convert it into JSON (and removing all those keys that have value as NULL)..

When i use a given Mule Transformer , ObjectToJSON, it is converting into appropriate JSON..but not able to remove NULL value..And i could not find any properties to set in Custom-transformer that will remove NULL values..!!

So then, i wrote a custom transformer, that uses the net.sf.json-lib library and i am able to remove NULL values.

But in one of my JAVA Object , i have a HashMap<Integer,String> and since in JSON Object , Integer cannot be keys, net.sf.json library is giving an Exception :

Exception stack is:
1. JSON keys must be strings. (java.lang.ClassCastException)
  net.sf.json.JSONObject:1120 (null)
2. java.lang.ClassCastException: JSON keys must be strings. (net.sf.json.JSONException)
  net.sf.json.JSONObject:1160 (null)
3. java.lang.ClassCastException: JSON keys must be strings. (net.sf.json.JSONException). Message payload is of type: HashMap (org.mule.api.transformer.TransformerMessagingException)

and so it is unable to convert it into JSON..

So what is most viable option..??

share|improve this question
If JSON doesn't allow keys to be a number, can start your keys with an _ e.g. ` { _1: Hello, _9: World } ` –  Peter Lawrey Mar 15 '12 at 13:09
No..i have already made my JAVA code..and i cannot make changes in it..Secondly, i have to given Integer to my front end APP.. (although it can be an Integer String like "1") ..!!..please suggest what to do..!! –  Ramandeep Singh Mar 15 '12 at 13:13
add comment

3 Answers

I would recommend you to try gson it worked like a magic for me.

Collections Examples

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);


String json = gson.toJson(ints); ==> json is [1,2,3,4,5]


Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);

ints2 is same as ints

Here is an example of how to write a custom serializer for JodaTime DateTime class.

private class DateTimeSerializer implements JsonSerializer<DateTime> {
  public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext  context) {
    return new JsonPrimitive(src.toString());
share|improve this answer
i have looked at it and i found two issues...First of all, it does not covert SubObjects to JSON And secondly, i was not able to Parse Datetime ..The setDateFormat property does not work in gson..it is reported bug in gson library..!! –  Ramandeep Singh Mar 15 '12 at 13:06
@RamandeepSingh, what is 'SubObject'? The Date issue was fixed in the recent verison and anyway you can write your own de/serializer - see my edit. –  aviad Mar 15 '12 at 13:12
subObjects : I have HashMap<String,Object> ..Now Object can be another HashMap<String,Object> which will considered as SubObject..!! –  Ramandeep Singh Mar 15 '12 at 13:15
so why to use raw types in the first place? and BTW you can write your own de/serializer which checks the type and treats the complex type properly. –  aviad Mar 15 '12 at 13:16
Well, I fail to imagine the problem that justifies such complex hierarchy. Maybe you can elaborate on what are you trying to do? Anyway I would suggest you to consider redesign :) –  aviad Mar 15 '12 at 14:43
show 3 more comments

Have you looked at Gson?


share|improve this answer
look my comment for other answer suggesting gson ..!! –  Ramandeep Singh Mar 15 '12 at 13:08
add comment

From http://www.ietf.org/rfc/rfc4627.txt


I would suggest to modify your initial Java structure to use String as key type.

However with Jackson library you can create fancier solutions:

  1. Use a custom deserializer Deserializing non-string map keys with Jackson

  2. Use a Tree Model instead of your own Java POJO http://wiki.fasterxml.com/JacksonInFiveMinutes

share|improve this answer
add comment

Your Answer


