내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

Need JSON in Value Format

Avatar

Level 4

Hi All,

 

I have written below JAVA  to get my JSON response from Content Fragment.

 

 

public static JsonArray getMultiValueAsArray(String contentPath, Value[] values, ResourceResolver resourceResolver) throws RepositoryException {
		JsonArray array = new JsonArray();
		if (values != null) {
			for (Value propertyValue : values) {
				JsonParser jsonParser = new JsonParser();
				JsonElement jsonObj = jsonParser.parse(propertyValue.getString());
				logger.info("jsonObj inside getMultiValueAsArray ===== "+jsonObj);
				array.add(jsonDataParser(contentPath, jsonObj, resourceResolver));
			}
		}
		return array;
	}

 

 

And my JSOn is coming below. Here my multifield is storing as STRING [] array , as shown below.

subnaik_2-1686884930346.png

 

subnaik_0-1686884757906.png

But here my requirement is that I want my JSOn format should come like below.

 

subnaik_1-1686884828148.png

Can somebody please let me know what kind of changes I need to do in my JAVA so the format will come like above.

 

Thanks & Regards

Subrat

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor

@subnaik 

 

Option-1:

You will have to use a nested JsonObject to get the desired structure.

 

 

    public static JsonArray getMultiValueAsArray(String contentPath, Value[] values, ResourceResolver resourceResolver) throws RepositoryException {
        JsonArray array = new JsonArray();
        if (values != null) {
            for (Value propertyValue : values) {
                JsonObject propertyObj = new JsonObject();
                JsonObject nestedObj = new JsonObject();
                nestedObj.addProperty("value", propertyValue.getString());
                propertyObj.add(propertyValue, nestedObj);
                array.add(nestedObj);
            }
        }
        return array;
    }

 

Here:  

propertyObj is {"byline": {"value", "test2"}} and nestedObj is  {"value", "test2"}

Option-2: Usage of bean classes. Easier to understand and construct structure.

https://stackoverflow.com/questions/11692303/turning-a-bean-to-json-in-java


Aanchal Sikka

원본 게시물의 솔루션 보기

3 답변 개

Avatar

Community Advisor

Hi @subnaik 

You should create a POJO class according to the required JSON object and populate value from multifield object to newly created POJO. After that, you can convert the populated POJO class into JSON.

 

Regards,

Arpit Varshney

Avatar

정확한 답변 작성자:
Community Advisor

@subnaik 

 

Option-1:

You will have to use a nested JsonObject to get the desired structure.

 

 

    public static JsonArray getMultiValueAsArray(String contentPath, Value[] values, ResourceResolver resourceResolver) throws RepositoryException {
        JsonArray array = new JsonArray();
        if (values != null) {
            for (Value propertyValue : values) {
                JsonObject propertyObj = new JsonObject();
                JsonObject nestedObj = new JsonObject();
                nestedObj.addProperty("value", propertyValue.getString());
                propertyObj.add(propertyValue, nestedObj);
                array.add(nestedObj);
            }
        }
        return array;
    }

 

Here:  

propertyObj is {"byline": {"value", "test2"}} and nestedObj is  {"value", "test2"}

Option-2: Usage of bean classes. Easier to understand and construct structure.

https://stackoverflow.com/questions/11692303/turning-a-bean-to-json-in-java


Aanchal Sikka

Avatar

Community Advisor and Adobe Champion

Hi @subnaik ,

 

To be true, the current JSON structure is more promising rather than the new one you want to achieve. It is less in size, more readable and easy to traverse. I would recommend you check the business requirement and try to propose the older one. 

 

Completing task is 'Job done', completing it right way is 'Achievement'. 🙂

 

Thanks,

Ritesh Mittal