Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Need to return JsonObject from a getter in sling model exporter

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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;
}

View solution in original post

9 Replies

Avatar

Level 5

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

Avatar

Level 5

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.

Avatar

Correct answer by
Community Advisor

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;
}

Avatar

Level 5

Thanks Kishore. It worked and was a good learning about object node.

Avatar

Level 5

Thanks Santosh for sharing this article. It was a good learning in it and was very helpful.

Avatar

Community Advisor

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());
         }
     }

Vijayalakshmi_S_0-1634839481183.png

 

Avatar

Level 5

Thanks @Vijayalakshmi_S . It is working solution for the problem. And It was a great learning about "JsonRawValue" annotation.