Need to return JsonObject from a getter in sling model exporter | Community
Skip to main content
Ravi_Joshi
Level 4
October 21, 2021
Solved

Need to return JsonObject from a getter in sling model exporter

  • October 21, 2021
  • 5 replies
  • 2639 views

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.
Best answer by Kishore_Kumar_

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

5 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
October 21, 2021

Hi Ravi,

 

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

 

Thanks

Ravi_Joshi
Level 4
October 21, 2021

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

Ravi_Joshi
Level 4
October 21, 2021

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.

Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
October 21, 2021

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;
}
Ravi_Joshi
Level 4
October 22, 2021

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

SantoshSai
Community Advisor
Community Advisor
October 21, 2021
Ravi_Joshi
Level 4
October 22, 2021

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

Vijayalakshmi_S
Level 10
October 21, 2021

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

 

Ravi_Joshi
Level 4
October 22, 2021

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