Converting AEM/Sling Resources to JSON

Adobe Experience Manager | AEM/CQ | Apache Sling

Converting AEM/Sling Resources to JSON

You can easily convert an AEM Page, Sling Resource, or JCR Node to JSON using the org.apache.sling.commons.json.jcr.JsonItemWriter class. This simply but useful utility allows you to dump a Node into a JSONObject. It also allows you to dump the Node into a JSON string into a PrintWriter for use in servlets for example.

The constructor accepts a Set of Strings representing JCR properties to exclude from the JSON output. You may want to ignore the standard cq:*, sling:* and jcr:* properties while allowing only your custom properties to populate the JSON.

The overloaded dump methods allow you to pass in the recursion level similar to how you would use a selector when making AJAX calls to the Default Get Servlet. Just as you can cURL /content/geometrixx/en.json, /content/geometrixx/en.1.json and /content/geometrixx/en.-1.json, you can pass in a positive integer for the recursion level as well as -1 for infinite recursion.

Likewise, just as you can cURL /content/geometrixx/en.tidy.json, you can specify whether you want the JSON output nicely formatted or not.

The following examples demonstrate the JsonItemWriter utilizing a PrintWriter in a servlet and a JSONObject in a standard Java class.




25 comments

sudhanshu Sharma | June 14, 2017 at 06:39 AM | Reply

Very nice explanation and great piece of knowledge. Thanks alot.

Sceptic | April 24, 2019 at 01:14 PM | Reply

And `ResourceResolver` is a parameter because...?

Nikhil Kumar | October 04, 2019 at 12:13 PM | Reply

Following is the solution for getting data from DAM and writing the Data. https://aemsimplifiedbynikhil.wordpress.com/2019/10/04/read-write-data-in-json-file-of-dam-in-aem-making-rest-api-call/

AvereottTecemog | December 14, 2019 at 11:19 PM | Reply

fuse hemp oil <a href="http://cdbhempoilpro.com/#">benefits of hemp oil for humans</a> cannabis cream

AvereottTecemog | December 17, 2019 at 11:54 AM | Reply

cbd cream amazon <a href="http://cdbhempoilpro.com/#">cbd pure hemp oil</a> cannabis oil for seizures

AvereottTecemog | December 17, 2019 at 06:31 PM | Reply

cbd daily intensive cream <a href="http://cdbhempoilpro.com/#">hemp oil cbd</a> 1000 mg cbd gummies

AvereottTecemog | December 17, 2019 at 08:31 PM | Reply

pch slots tournament <a href="http://onlinecasinoslott.com/#">free casino</a> pala casino online

AvereottTecemog | December 17, 2019 at 08:58 PM | Reply

play free slot machines with bonus spins <a href="http://onlinecasinoslott.com/#">online casino</a> vegas casino games slots free

AvereottTecemog | December 17, 2019 at 10:25 PM | Reply

online casino reviews <a href="http://onlinecasinoslott.com/#">no deposit casino</a> online casinos for us players

AvereottTecemog | December 18, 2019 at 06:19 AM | Reply

casino games slots free <a href="http://onlinecasinoslott.com/#">casino play</a> virgin online casino

AvereottTecemog | December 18, 2019 at 09:02 PM | Reply

absolutely free casino slots games <a href="http://onlinecasinoslott.com/#">casino bonus codes</a> liberty slots

Swati | December 30, 2019 at 11:10 AM | Reply

Is there any option to ignore all jcr and cq properties while taking the dump of a node instead of manually adding each and every jcr property? I could not find wildcard support in JSONItemWriter for "jcr:*", "cq:*" Thanks

Jusdesadvosse | February 11, 2020 at 02:12 PM | Reply

<a href="http://cbdoilwalm.com/">buy cbd </a> <a href="http://cbdoilwalm.com/">cbd drops </a> cbd

Jusdesadvosse | February 12, 2020 at 08:30 PM | Reply

<a href=" https://cbdoilww.com/ ">cbd oil cbd vape </a> <a href="https://cbdoilww.com/">cbd oil yaa health store </a> <a href="https://cbdoilww.com/">cbd austin </a> <a href="https://cbdoilww.com/">walgreens cbd oil price </a>

unesmaftranna | February 12, 2020 at 08:48 PM | Reply

<a href="https://cbdoilww.com/">green roads cbd </a> <a href="https://cbdoilww.com/">cbd ointment for pain </a> cbd vape oil <a href="">vape cbd </a> <a href=" https://cbdoilww.com/ ">cbd vape cartridges </a>

Jusdesadvosse | February 12, 2020 at 10:49 PM | Reply

<a href=" https://cbdoilww.com/ ">cbd pure hemp oil </a> <a href="https://cbdoilww.com/">colorado cbd oil </a> <a href="https://cbdoilww.com/">what is cbd oil good for </a> <a href="https://cbdoilww.com/">cbd salve for pain </a>

unesmaftranna | February 12, 2020 at 11:06 PM | Reply

<a href="https://cbdoilww.com/">cbd topicals for pain </a> <a href="https://cbdoilww.com/">cannativa cbd oil </a> charlotte's web cbd oil <a href="">defy cbd drink </a> <a href=" https://cbdoilww.com/ ">cbd isolate </a>

Jusdesadvosse | February 13, 2020 at 09:09 AM | Reply

<a href=" https://cbdoilww.com/ ">cbd oil for cats </a> <a href="https://cbdoilww.com/">buycbdoilcure best cbd oil </a> <a href="https://cbdoilww.com/">cbd hemp </a> <a href="https://cbdoilww.com/">cbd lotion/creams </a>

unesmaftranna | February 13, 2020 at 09:42 AM | Reply

<a href="https://cbdoilww.com/">how to use cbd oil </a> <a href="https://cbdoilww.com/">cali naturals cbd oil reviews </a> cbd online <a href="">trubliss cbd </a> <a href=" https://cbdoilww.com/ ">cbd pure </a>

Jusdesadvosse | February 13, 2020 at 01:04 PM | Reply

<a href=" https://cbdoilww.com/ ">cbd gummies </a> <a href="https://cbdoilww.com/">hemp cbd </a> <a href="https://cbdoilww.com/">cbd drops </a> <a href="https://cbdoilww.com/">cbd vape </a>

Jusdesadvosse | February 13, 2020 at 08:27 PM | Reply

<a href=" https://cbdoilww.com/ ">cbd gummies </a> <a href="https://cbdoilww.com/">buy cbd oil online </a> <a href="https://cbdoilww.com/">buy cbd oil online </a> <a href="https://cbdoilww.com/">buy hemp </a>

unesmaftranna | February 13, 2020 at 09:08 PM | Reply

<a href="https://cbdoilww.com/">cbd tinctures </a> <a href="https://cbdoilww.com/">hemp cbd </a> buy hemp <a href="">cbd vape </a> <a href=" https://cbdoilww.com/ ">buy hemp oil </a>

Jusdesadvosse | February 14, 2020 at 01:01 AM | Reply

<a href=" https://cbdoilww.com/ ">buy hemp </a> <a href="https://cbdoilww.com/">buy cbd </a> <a href="https://cbdoilww.com/">buy cbd oil </a> <a href="https://cbdoilww.com/">cbd tinctures </a>

Jusdesadvosse | February 14, 2020 at 05:34 AM | Reply

<a href=" https://cbdoilww.com/ ">cbd online </a> <a href="https://cbdoilww.com/">buy hemp oil </a> <a href="https://cbdoilww.com/">cbd tinctures </a> <a href="https://cbdoilww.com/">buy cbd oil online </a>

Penni | March 17, 2020 at 08:03 PM | Reply

online casino games free casino games casino slots [url=http://casinorealmoneysoe.com]casino slots[/url] no deposit casino

Leave a Comment