How to fetch multifield item's content from the below JSON in junits | Community
Skip to main content
Level 3
July 11, 2024
Solved

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

  • July 11, 2024
  • 2 replies
  • 702 views

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

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 MukeshYadav_

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,

2 replies

MukeshYadav_
Community Advisor
MukeshYadav_Community AdvisorAccepted solution
Community Advisor
July 11, 2024

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,

priyak-1Author
Level 3
July 11, 2024

Thank you so much, this has worked!

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
July 11, 2024

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