Iterating a JSON using Jackson-Databind Library like JDOM for XML

Technology Blog

Iterating a JSON using Jackson-Databind Library like JDOM for XML

I recently came across a situation that required me to be able to iterate over a JSON message payload similar to what can be done with JDOM in regards to XML similar to what I do within my Stax XML Mapreduce InputFormat.  So basically in this case you need to treat JSONArray’s similar to XML ChildElements and JSONObjects as an Element or Attribute. Jackson-databind javadoc and Jackson-databind maven package.

See Example code on how to iterate a JSONObject or a JSONArray:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Iterator;

import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonTester {

public static void main(String[] args) throws IOException {

ObjectMapper mapper = new ObjectMapper();

JsonNode rootNodeOrig = mapper.readTree(readJsonFile(“/tmp/json2.txt”));

JsonNode rootNode = mapper.readTree(readJsonFile(“/tmp/json2.txt”));

if (rootNode.isArray()) {

iterateArrElements(rootNode);

} else if (rootNode.isObject()) {

iterateObjFields(rootNode);

}

System.out.println(“rootNodeNew: “+rootNode.toString());

System.out.println(“RootNodeOrig: “+rootNodeOrig.toString());

}

public static void iterateObjFields(JsonNode jsonNode) {

Iterator<Map.Entry<String, JsonNode>> fieldIter = jsonNode.fields();

ObjectNode parentObjectNode = (ObjectNode) jsonNode;

while (fieldIter.hasNext()) {

Map.Entry<String, JsonNode> field = fieldIter.next();

System.out.println(“Object/field.getKey(): ” + field.getKey());

if (field.getValue().isContainerNode()) {

if (field.getValue().isArray()) {

iterateArrElements(field.getValue());

} else if (field.getValue().isObject()) {

iterateObjFields(field.getValue());

}

} else {

if (field.getKey().equalsIgnoreCase(“type”)) {

parentObjectNode.put(field.getKey(), field.getValue().asText()+”_trash”);

}

System.out.println(“Object/field.getValue(): ” + field.getValue());

}

}

}

public static void iterateArrElements(JsonNode jsonNode) {

Iterator<JsonNode> elementsIter = jsonNode.elements();

while (elementsIter.hasNext()) {

JsonNode element = elementsIter.next();

System.out.println(“ElementArray”);

if (element.isContainerNode()) {

if (element.isArray()) {

iterateArrElements(element);

} else if (element.isObject()) {

iterateObjFields(element);

}

}

}

}

public static String readJsonFile(String file) {

File f = new File(file);

if (f.exists()) {

InputStream is = null;

try {

is = new FileInputStream(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String jsonTxt = null;

try {

jsonTxt = IOUtils.toString(is);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return jsonTxt;

}

return null;

}