How to migrate page content in JSON format to AEM JCR

Avatar

Avatar

jawaharb

Avatar

jawaharb

jawaharb

15-09-2020

We have custom built CMS and the page content is actually stored in JSON format. The content structure of these JSON files are very specific to our CMS. Now we need to migrate these pages into AEM. What is the best options/practices available to do these kind of content migration.

We can actually convert those JSON files to corresponding XML files (as AEM JCR expecting), but what are the options available to import those into AEM JCR.

AEM 6.5.4 JCR migration xml
View Entire Topic

Avatar

Avatar

raj_mandalapu

Avatar

raj_mandalapu

raj_mandalapu

15-09-2020

You can build a package by including your XML content, update filters and deploy using the package manager, to build a package you can follow either manually or via code, if it is a one time operation then the manual approach is ok.

You can also modify your JSON as JCR expected JSON format and import via curl command. 

https://gist.github.com/joemaffia/87f8328eed1810a93260

if you want to have great control over the pages which you are going to create then you need to go with page manager API's, you don't need to convert your JSON to XML, instead of that you can directly read your exported JSON and create pages.

Sample code:

String pagePath = "/content/toolkit/en/";
		String pageName = "home";
		String templatePath = "/apps/toolkit/templates/page-home";
		String pageTitle = "home page";
		Page newPage;
		ResourceResolverFactory resourceResolverFactory = getSlingScriptHelper()
				.getService(ResourceResolverFactory.class);
		Map<String, Object> authInfo = new HashMap<>();
		authInfo.put(ResourceResolverFactory.SUBSERVICE, "createService");
		ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(authInfo);
		Session session = resolver.adaptTo(Session.class);
		try {
			newPage = getPageManager().create(pagePath, pageName, templatePath, pageTitle);
			if (newPage != null) {
				user = resolver.getUserID();
				Node newNode = newPage.adaptTo(Node.class);
				Node cont = newNode.getNode("jcr:content");
				if (cont != null) {
					Node rootNode = session.getRootNode();
					String path = rootNode.getPath();
					Node parNode = JcrUtils.getNodeIfExists(cont, "par");
					Node imageNode = JcrUtils.getOrCreateByPath(parNode.getPath() + "/image",
							JcrConstants.NT_UNSTRUCTURED, session);
					Node textNode = JcrUtils.getNodeIfExists(parNode, "text");
					imageNode.setProperty("sling:resourceType", "foundation/components/image");
					imageNode.setProperty("fileReference", "/content/dam/we-retail-screens/we-retail-instore-logo.png");
					textNode.setProperty("text", "<p>This page is created using page manager</p>");
					session.save();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}