Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to fetch multifield item's content from the below JSON in junits

Avatar

Level 4

Can anyone help me with the below query?

In the below JSON, how can I fetch desc and title from the multiFieldContent ?? with the help of JSONArray we can achieve it, but since its deprecated.. please provide any suggestions.

 

{
"jcr:primaryType": "nt:unstructured",
"textLabel": "Text",
"multiFieldContent": {
"jcr:primaryType": "nt:unstructured",
"item0": {
"jcr:primaryType": "nt:unstructured",
"desc": "desc1",
"title": "title1"
},
"item1": {
"jcr:primaryType": "nt:unstructured",
"desc": "desc2",
"title": "title2"
}
}
}

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

We can use Gson, however you can check here how to write junit test for multifield https://medium.com/@veena.vikraman19/aem-junit-for-simple-aem-component-with-multifield-2326f61c8982  

 

import com.google.gson.Gson;
import com.google.gson.JsonObject;

String str="{\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"textLabel\": \"Text\",\n"

+ " \"multiFieldContent\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"item0\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"desc\": \"desc1\",\n"

+ " \"title\": \"title1\"\n"

+ " },\n"

+ " \"item1\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"desc\": \"desc2\",\n"

+ " \"title\": \"title2\"\n"

+ " }\n"

+ " }\n"

+ "}";

 

JsonObject json=new Gson().fromJson(str, JsonObject.class);

 

JsonObject multiFieldContent=json.get("multiFieldContent").getAsJsonObject();

 

String desc1=multiFieldContent.get("item0").getAsJsonObject().get("desc").getAsString();

 

String desc2=multiFieldContent.get("item1").getAsJsonObject().get("desc").getAsString();

 

 

Thanks,

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi,

We can use Gson, however you can check here how to write junit test for multifield https://medium.com/@veena.vikraman19/aem-junit-for-simple-aem-component-with-multifield-2326f61c8982  

 

import com.google.gson.Gson;
import com.google.gson.JsonObject;

String str="{\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"textLabel\": \"Text\",\n"

+ " \"multiFieldContent\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"item0\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"desc\": \"desc1\",\n"

+ " \"title\": \"title1\"\n"

+ " },\n"

+ " \"item1\": {\n"

+ " \"jcr:primaryType\": \"nt:unstructured\",\n"

+ " \"desc\": \"desc2\",\n"

+ " \"title\": \"title2\"\n"

+ " }\n"

+ " }\n"

+ "}";

 

JsonObject json=new Gson().fromJson(str, JsonObject.class);

 

JsonObject multiFieldContent=json.get("multiFieldContent").getAsJsonObject();

 

String desc1=multiFieldContent.get("item0").getAsJsonObject().get("desc").getAsString();

 

String desc2=multiFieldContent.get("item1").getAsJsonObject().get("desc").getAsString();

 

 

Thanks,

Avatar

Community Advisor

Hi, 

If you need this to be just for your JUnit, you could use any other JSON parser as a dependency to do the job. For example, you can use the Jackson library to represent the JSON data and get those values. Please check this: https://www.baeldung.com/java-jsonnode-get-keys 

 

For example:

- Include the dependency in your pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
    <scope>test</scope>
</dependency>

- Try something like this

//Your JSON string representation
String jsonString = "{}";
// Create ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();

// Parse JSON string to JsonNode
JsonNode rootNode = mapper.readTree(jsonString);

// Navigate to the multiFieldContent node
JsonNode multiFieldContentNode = rootNode.path("multiFieldContent");

// Iterate over items in multiFieldContent
multiFieldContentNode.fields().forEachRemaining(entry -> {
    JsonNode itemNode = entry.getValue();
    String title = itemNode.path("title").asText();
    String desc = itemNode.path("desc").asText();
    System.out.println("Title: " + title + ", Description: " + desc);
});

 

Hope this helps.


Esteban Bustamante