I\'m a beginning java programmer, so I\'m sorry if my question is kind of dumb.
I have a JSON object that looks like this:
{
\"element1\" : {
\"g
Check this
How to not send an empty collection in jackson
For empty node in Json you can use
http://jackson.codehaus.org/1.1.2/javadoc/org/codehaus/jackson/node/ObjectNode.html#remove(java.lang.String)
Removing JSON elements with jackson
These can solve your problem.
I haven't tested it, but you probably want something like this:
public static void stripEmpty(JsonNode node) {
Iterator<JsonNode> it = node.iterator();
while (it.hasNext()) {
JsonNode child = it.next();
if (child.isObject() && child.isEmpty(null))
it.remove();
else
stripEmpty(child);
}
}
I want only to show direction how to do that with Json:
JSONObject jsonObj = new JSONObject(_message);
Map<String, JSONObject> map = jsonObj.getMap();
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
JSONObject o = map.get(key);
if(o.length() == 0){
it.remove();
}
}
When JSONObject
loads {}
its length is 0, therefore you can drop it.
As A side note, you can use this method in recursion like:
JSONObject jsonObj = new JSONObject(_message);
invoke(jsonObj);
...
private static void invoke(JSONObject jsonObj) {
Map<String, JSONObject> map = jsonObj.getMap();
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
JSONObject o = map.get(key);
if(o.length() == 0){
it.remove();
}
else{
invoke(o);
}
}
}
I didn't add any validation but for sure , you need to verify instance of jsonObj.getMap()
...