Converting AEM JCR nodes into JSON

AEM resources such as JCR nodes can be easily converted into JSON format using class which extends class.

The dump() method of TidyJsonItemWriter class dumps the given node in JSON, optionally recursing into its child nodes and allows us to specify recursion level (-1 for infinity) and produces JSON in a nicely formatted way if the tidy parameter is passed as true.

Method details:

public void dump(javax.jcr.Node node,
Writer w,
int maxRecursionLevels,
boolean tidy)
throws javax.jcr.RepositoryException,


maxRecursionLevels: -1 for infinity recursion
tidy: if true the json dump is nicely formatted


Sample code:

Node node = session.getNode(“/apps/geometrixx-media/components/advancedtext”);
StringWriter stringWriter = new StringWriter();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
jsonWriter.dump(node, stringWriter, -1, true);
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); //disableHtmlEscaping in JSON
return gson.toJson(stringWriter.toString());

Sample output:

I hope this blog post is informative. Thanks for reading!

About The Author