Create JCR nodes from json string | Community
Skip to main content
Level 4
October 16, 2015
Solved

Create JCR nodes from json string

  • October 16, 2015
  • 5 replies
  • 3440 views

Hi All , 

  Wanted to know if there are any utility classes which can be used to create jcr nodes from json string ? I actually would execute a rest service which would return a proper json response with key value pairs . I would have normally parsed the whole json add properties required  for jcr like  'node type as unstructured property '  and execute curl by passing in a sling servlet url which would return this json response . 

However i was wondering if I can use any utility classes which can off load parsing and adding necessary properties required for jcr  ?

Thanks in advance

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Shivalingaiah

below is the steps I followed

 

Bundle A

1. Convert JSON to Object o1 using jackson (annotation based)

2. pass o1 to bundle B

Bundle B

1. Map the object o1 to Object o2 which is annotated for Jcrom (This was needed as there was some business logic needed before storing it in nodes)

2. convert o2 to jcr nodes using JCROM (annotation based)

In this way Bundle B can accept object from any bundles. Convert/Map them to the object value which needs to be stored.

Hope it helps/solves your need...

5 replies

smacdonald2008
Level 10
October 16, 2015

Write a custom OSGi service that parses JSON and then use the JCR API to create nodes. 

Lokesh_Shivalingaiah
Level 10
October 16, 2015

I had a similar requirement and implemented as below

This can be done in 2 steps using utilities.

Step 1:  convert JSON to Java object using 'GSON' or 'Jackson'

Step 2: convert java object to jcr node using 'Jcrom'

'This helped me in modulerizing as JSON was the response from one bundle and its consumed by another bundle.

 

 

~Loki

KrisgumAuthor
Level 4
October 16, 2015

 This seems to be very good approach as we would be dealing with objects . JCrom normally relies on annotations on java objects to convert to jcr nodes  . So when you get your objects from GSON what were you using to get them annotated ? 

Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
October 16, 2015

below is the steps I followed

 

Bundle A

1. Convert JSON to Object o1 using jackson (annotation based)

2. pass o1 to bundle B

Bundle B

1. Map the object o1 to Object o2 which is annotated for Jcrom (This was needed as there was some business logic needed before storing it in nodes)

2. convert o2 to jcr nodes using JCROM (annotation based)

In this way Bundle B can accept object from any bundles. Convert/Map them to the object value which needs to be stored.

Hope it helps/solves your need...

KrisgumAuthor
Level 4
October 16, 2015

Thanks for sharing . Nice approach and always dealing with objects is the best part.