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.
SOLVED

Getting properties from node and storing in String[]

Avatar

Level 2

Hi,

 

I have a component with multifiled (testAppIDs). The multifield data gets stored in form of nodes after Coral 3 migration (item0,item1 etc.). 

ks_hitijingole_0-1623251491719.png

 

Earlier before Coral 3 Migration Model class had a logic to use the JSON array. I want the model class to use the same logic as the impact might be huge. So now after Coral 3 migration, as I cannot pass testAppIDs using @ValueMapValue , I am trying to create the same String[] by iterating through child node of testAppIDs. 

ks_hitijingole_1-1623251856057.png

Can you help me understand how can I fetch property and value data from the item nodes and put it in testAppIds String []. Sample Data Expected to be stored in testAppIds ->  [{"tapID":"TAP_10050"}, {"tapID":"TAP_10049"}, {"tapID":"TAP_10050"}]

1 Accepted Solution

Avatar

Correct answer by
Employee
// using gson JsonObject

List<String> tapIds = new ArrayList<String>();

for(Resource item : items.getChildren()) {
     JsonObject itemObject = new JsonObject();
     itemObject.addProperty("tapID", item.getValueMap().get("tapID", String.class));
     tapIds.add(itemObject.toString());
}

return tapIds.toArray(new String[tapIds.size()]);

Should be able to do something like that? i wrote that in the comment field itself, so there might be some syntax issues ... but should give you the gist. You can do it more concisely in a stream, but that's just style.

View solution in original post

1 Reply

Avatar

Correct answer by
Employee
// using gson JsonObject

List<String> tapIds = new ArrayList<String>();

for(Resource item : items.getChildren()) {
     JsonObject itemObject = new JsonObject();
     itemObject.addProperty("tapID", item.getValueMap().get("tapID", String.class));
     tapIds.add(itemObject.toString());
}

return tapIds.toArray(new String[tapIds.size()]);

Should be able to do something like that? i wrote that in the comment field itself, so there might be some syntax issues ... but should give you the gist. You can do it more concisely in a stream, but that's just style.