Hi,
I have a requienment where I need to return json object from one getter method of sling model exporter. please suggest your thoughts.
Thanks,
Ravi Joshi
Solved! Go to Solution.
Views
Replies
Total Likes
hi @Ravi_Joshi ,
Use Jackson API to return JSON Getters. Something like below.
public ObjectNode getJSONFields() { ObjectMapper mapper = new ObjectMapper(); ObjectNode objNode = mapper.createObjectNode(); objNode.put("key", "value"); return objNode; }
Hi Ravi,
Please check http://www.sgaemsolutions.com/2017/06/sling-model-exporter-in-aem-63.html and see if that helps?
Thanks
Hi Shiva,
This article is really helpful. But I would like to add more points on my requirenment.
Like we have an exporter and that exporter has a method say "public JSONobject getJson()" and it is returning a json object. And when I call exporter it will give me the json returned from the "getJson" as "'json':'{{JSON string from the getJson method}}'". Now I was thinking if we can replace the string as json object only like " 'json':{{JSON string from the getJson method}} " this.
Please let me know if you have any thoughts on the same.
Thanks,
Ravi Joshi
I would like to add more points on my requirenment.
Like we have an exporter and that exporter has a method say "public JSONobject getJson()" and it is returning a json object. And when I call exporter it will give me the json returned from the "getJson" as "'json':'{{JSON string from the getJson method}}'". Now I was thinking if we can replace the string as json object only like " 'json':{{JSON string from the getJson method}} " this.
Please let me know if you have any thoughts on the same.
hi @Ravi_Joshi ,
Use Jackson API to return JSON Getters. Something like below.
public ObjectNode getJSONFields() { ObjectMapper mapper = new ObjectMapper(); ObjectNode objNode = mapper.createObjectNode(); objNode.put("key", "value"); return objNode; }
Thanks Kishore. It worked and was a good learning about object node.
Hi @Ravi_Joshi
Kindly review https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/develop-slin...
hope this helps you!
Regards,
Santosh
https://www.techinnovia.com/blog/
Thanks Santosh for sharing this article. It was a good learning in it and was very helpful.
Hi @Ravi_Joshi,
You can use @JsonRawValue Jackson annotation(com.fasterxml.jackson.annotation.JsonRawValue) to the desired field.
JSONObject is from org.json.* family
@Getter @JsonRawValue private String json; @PostConstruct protected void init(){ /* Populating JSONObject */ JSONObject jsonObj = new JSONObject(); try { jsonObj.put("jsonKey", "jsonValue"); jsonObj.put("jsonKey1", "jsonValue1"); json = jsonObj.toString(); } catch (JSONException e) { LOG.error("JSONException={}", e.getMessage()); } }
Thanks @Vijayalakshmi_S . It is working solution for the problem. And It was a great learning about "JsonRawValue" annotation.
Views
Likes
Replies
Views
Likes
Replies