Export Plone contents to JSON

Hi,

I have been trying to find ways to export Plone contents to JSON format. I installed json.api.core which allows me to use the browser to input the specific content (portal_type) and results are displayed in raw json format. However, it's manual and I need an automatic way to export all portal types via Python script.

This page (https://docs.plone.org/4/en/develop/plone/content/importexport.html) provides the code example for JSON export. However, I copied the code to export.py and tried to execute via command line but it's not working. I don't know if I pass in the path properly as it throws an error when making this function call unrestrictedTraverse. Has anyone had experience with the code before and are able to get it to work? It would be great if someone can provide detailed steps.

Thanks!

collective.jsonify is easy to use: https://collectivejsonify.readthedocs.io/en/latest/

Basic steps:

  • add collective.jsonify to the buildout
  • add an External Method in the ZMI
  • go to http://localhost:8080/Plone/get_children (and wait, helps to watch the log to know when it's done)
  • JSON is ready! You will likely have to check the logs to see where the script put the JSON files, from my local instance they were put into a /tmp/ directory.
1 Like

Thanks! Tried your suggestion and I was able to export the contents by typing http://localhost:8080/Plone/export_content from the browser. However, I prefer to call the python script directly without the browser. I am also learning how to set up, compile and execute python code properly. Basically, I copied export.py from https://docs.plone.org/4/en/develop/plone/content/importexport.html, do you know where I should copy it to, and how to get the script to work?

You have at least two options:

  1. HTTP automation
    I tend to use wget for this; but you could also use curl or a Python script using the urllib[2] module.
  2. A "Plone script" startet with bin/{instance|client1} run.
    Such a script has programmatic access to the complete Zope hierarchy. export_content is likely a view, registered in some zcml file. After some setup (you need to partially emulate the initial Web request processing), you can call this view programmatically.

The second approach needs more internal knowledge. It has the advantage, that you do not need to expose potentially sensitive passwords.

For an example on how to set up a script that emulates a normal web request, see this page from the Plone Documentation:

https://docs.plone.org/develop/plone/misc/zope_debug_prompt.html

If you plan to use the example that @fredvd suggested, remember to add plone.api in your buildout or setup.py since it's not installed by default.