Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Only Show Static dropdown Value in JSON

Avatar

Level 3

Hi All,

 

I have developed one static dropdown with node name tabs order, as shown below

 

subnaik_0-1681829592336.png

 

subnaik_1-1681829791981.png

 

 

I have written below JAVA code.

 

*********************** TabOrderModel  JAVA Class ***********************

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class TabOrderModel {

String tabsOrderEnabled;

JsonArray tabsOrder;

public String getTabsOrderEnabled() {
return tabsOrderEnabled;
}

public void setTabsOrderEnabled(String tabsOrderEnabled) {
this.tabsOrderEnabled = tabsOrderEnabled;
}

public JsonArray getTabsOrder() {
return tabsOrder;
}

public void setTabsOrder(JsonArray tabsOrder) {
this.tabsOrder = tabsOrder;
}

@Override
public String toString() {

return "TabOrderModel [tabsOrderEnabled =" + tabsOrderEnabled + ", tabsOrder=" + tabsOrder + " ]";
}

}

 

****************************** JsonUtility JAVA *************************

 

private static void setTabOrderDetails(Node masterNode, ResourceResolver resourceResolver,
TabOrderModel tabOrderModel) throws RepositoryException {
if (masterNode.hasProperty(Constants.TABS_ORDER)) {
Property TabOrderProperty = masterNode.getProperty("tabsOrder");
//logger.info("values for TabOrderProperty ====== " + TabOrderProperty);//property=tabsOrder = [{"tabName":"community.about.value"}, {"tabName":"leadership.label.value"}, {"tabName":"community.about.value"},
multiValueTabOrder(resourceResolver, tabOrderModel, TabOrderProperty);
}
}

private static void multiValueTabOrder(ResourceResolver resourceResolver,
TabOrderModel tabOrderModel, Property TabOrderProperty) throws RepositoryException {

if (TabOrderProperty.isMultiple()) {
JsonArray array = new JsonArray();
JsonParser jsonParser = new JsonParser();
javax.jcr.Value[] values = TabOrderProperty.getValues();

for (javax.jcr.Value value : values) {
String jsonObj = value.getString();
logger.info("jsonObj123 for taborder ====== " + jsonObj);
array.add(jsonObj);
}

tabOrderModel.setTabsOrder(array);
}
}

 

But my JSON is coming below

subnaik_2-1681830395982.png

 

My requirement is that  ONLY tabName Value should come like below highlighted in red color but it is coming like above.

 

"tabsOrderSection": {
"tabsOrderEnabled": "true",
"tabsOrder": [
"community.about.value",
"leadership.label.value",
"community.about.value",
"leadership.label.value"
]
}

 

Can anybody please let me know how can I show only value of tabname inside JSON ?

 

Thanks

2 Replies

Avatar

Community Advisor

Hi @subnaik As you are returning the response via the servlet, the manipulation of the format required can be done in the business logic layer.
You can have the Json Array within the Json Object before returning it.

import org.json.*;
import java.util.*;
public class JSONArrayToJSONObjTest {
   public static void main(String args[]) {
      List<String> list = new ArrayList<String>();
      list.add("community.about.value");
      list.add("leadership.label.value");
      list.add("community.about.value");
      list.add("leadership.about.value");
      JSONArray array = new JSONArray();
      for(int i = 0; i < list.size(); i++) {
         array.put(list.get(i));
      }
      JSONObject obj = new JSONObject();
      try {
         obj.put("tabsOrder", array);
      } catch(JSONException e) {
         e.printStackTrace();
      }
      System.out.println(obj.toString());
   }
}

Hope this helps!

Thanks

Avatar

Level 3

Hi @Shailesh_Bassi_ 

 

Thanks for your reply but here my requirement is that only my value for tabName node dropdown should come like below

 

community.about.value 

 

Not like both dropdown node name and value will come like below.

 

tabName : community.about.value