Need to return JsonObject from a getter in sling model exporter | Community
Skip to main content
Ravi_Joshi
Level 4
Oct 21, 2021 10:26 AM UTC
Solved

Need to return JsonObject from a getter in sling model exporter

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
Oct 21, 2021 1:46 PM UTC

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

9 replies

Vijayalakshmi_S
Level 10
Oct 21, 2021 6:04 PM UTC

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

 

Translate
Ravi_Joshi
Level 4
Oct 22, 2021 5:59 AM UTC

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

Translate
SantoshSai
New Participant
Oct 21, 2021 2:50 PM UTC
Ravi_Joshi
Level 4
Oct 22, 2021 5:57 AM UTC

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

Translate
Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
Oct 21, 2021 1:46 PM UTC

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;
}
Translate
Ravi_Joshi
Level 4
Oct 22, 2021 5:55 AM UTC

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

Translate
Ravi_Joshi
Level 4
Oct 21, 2021 11:57 AM UTC

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.

Translate
Siva_Sogalapalli
New Participant
Oct 21, 2021 10:56 AM UTC

Hi Ravi,

 

Please check http://www.sgaemsolutions.com/2017/06/sling-model-exporter-in-aem-63.html and see if that helps?

 

Thanks

Translate
Ravi_Joshi
Level 4
Oct 21, 2021 11:56 AM UTC

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

Translate