Expand my Community achievements bar.

SOLVED

Create JCR nodes from json string

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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...

View solution in original post

5 Replies

Avatar

Level 10

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

Avatar

Level 10

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

Avatar

Level 4

 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 ? 

Avatar

Correct answer by
Level 10

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...

Avatar

Level 4

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