Expand my Community achievements bar.

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

Json Values in nested multifield

Avatar

Level 2

Hi,

 

Iam having a nested multifield and values are getting stored as json format in the jcr:content.However, '\' character is getting appended for the nested multifield values.

Json Values:

{"title":"page1","description":"this is description","ctaLinks":["{\"linkpath\":\"/content/maximintegrated/en/products\",\"linktitle\":\"title1\"}","{\"linkpath\":\"/content/maximintegrated/en\",\"linktitle\":\"title2\"}"],"ctaButtons":["{\"buttontitle\":\"button1\",\"buttonurl\":\"/content/maximintegrated\",\"btnStyle\":\"btn-primary\"}"]}

 

 

Can anyone say how to remove these characters in java and store it in json object.

 

Thanks,

Mahi

1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi @maheswariv26797 

Please use below code snippet to parse the given JSON string & to construct result JSON object.

 

JsonObject finalJson = new JsonObject();
JsonParser jsonParser = new JsonParser();

JsonObject jsonObject = jsonParser.parse("{\"title\":\"page1\",\"description\":\"this is description\",\"ctaLinks\":[\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en/products\\\",\\\"linktitle\\\":\\\"title1\\\"}\",\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en\\\",\\\"linktitle\\\":\\\"title2\\\"}\"],\"ctaButtons\":[\"{\\\"buttontitle\\\":\\\"button1\\\",\\\"buttonurl\\\":\\\"/content/maximintegrated\\\",\\\"btnStyle\\\":\\\"btn-primary\\\"}\"]}").getAsJsonObject();

if(jsonObject!= null) {
if (jsonObject.has("title")) {
finalJson.addProperty("title", jsonObject.get("title").getAsString());
}

if (jsonObject.has("description")) {
finalJson.addProperty("description", jsonObject.get("description").getAsString());
}

if (jsonObject.has("ctaLinks")) {
JsonArray ctaLinksJsonArr = jsonObject.get("ctaLinks").getAsJsonArray();

if (ctaLinksJsonArr != null) {
JsonArray resultCTALinksJsonArr = new JsonArray();
JsonObject ctaLinkJson;

for (int index = 0; index < ctaLinksJsonArr.size(); index++) {
ctaLinkJson = jsonParser.parse(ctaLinksJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLinkJson!= null) {
if (ctaLinkJson.has("linkpath")) {
ctaLinkJson.addProperty("linkpath", ctaLinkJson.get("linkpath").getAsString());
}

if (ctaLinkJson.has("btnstyle")) {
ctaLinkJson.addProperty("btnstyle", ctaLinkJson.get("btnstyle").getAsString());
}

resultCTALinksJsonArr.add(ctaLinkJson);
}
}

finalJson.add("ctaLinks", resultCTALinksJsonArr);
}
}

if (jsonObject.has("ctaButtons")) {
JsonArray ctaButtonsJsonArr = jsonObject.get("ctaButtons").getAsJsonArray();

if(ctaButtonsJsonArr!= null) {
JsonArray resultCTAButtonsJsonArr = new JsonArray();
JsonObject ctaLButtonJson;

for (int index = 0; index < ctaButtonsJsonArr.size(); index++) {
ctaLButtonJson = jsonParser.parse(ctaButtonsJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLButtonJson!= null) {
if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}

if (ctaLButtonJson.has("buttonurl")) {
ctaLButtonJson.addProperty("buttonurl", ctaLButtonJson.get("buttonurl").getAsString());
}

if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}
resultCTAButtonsJsonArr.add(ctaLButtonJson);
}
}

finalJson.add("ctaButtons", resultCTAButtonsJsonArr);
}
}
}

 

 

 

Result JSON (finalJson):

 

json.PNG

View solution in original post

5 Replies

Avatar

Level 8

Hi @maheswariv26797 

Please use below code snippet to parse the given JSON string & to construct result JSON object.

 

JsonObject finalJson = new JsonObject();
JsonParser jsonParser = new JsonParser();

JsonObject jsonObject = jsonParser.parse("{\"title\":\"page1\",\"description\":\"this is description\",\"ctaLinks\":[\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en/products\\\",\\\"linktitle\\\":\\\"title1\\\"}\",\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en\\\",\\\"linktitle\\\":\\\"title2\\\"}\"],\"ctaButtons\":[\"{\\\"buttontitle\\\":\\\"button1\\\",\\\"buttonurl\\\":\\\"/content/maximintegrated\\\",\\\"btnStyle\\\":\\\"btn-primary\\\"}\"]}").getAsJsonObject();

if(jsonObject!= null) {
if (jsonObject.has("title")) {
finalJson.addProperty("title", jsonObject.get("title").getAsString());
}

if (jsonObject.has("description")) {
finalJson.addProperty("description", jsonObject.get("description").getAsString());
}

if (jsonObject.has("ctaLinks")) {
JsonArray ctaLinksJsonArr = jsonObject.get("ctaLinks").getAsJsonArray();

if (ctaLinksJsonArr != null) {
JsonArray resultCTALinksJsonArr = new JsonArray();
JsonObject ctaLinkJson;

for (int index = 0; index < ctaLinksJsonArr.size(); index++) {
ctaLinkJson = jsonParser.parse(ctaLinksJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLinkJson!= null) {
if (ctaLinkJson.has("linkpath")) {
ctaLinkJson.addProperty("linkpath", ctaLinkJson.get("linkpath").getAsString());
}

if (ctaLinkJson.has("btnstyle")) {
ctaLinkJson.addProperty("btnstyle", ctaLinkJson.get("btnstyle").getAsString());
}

resultCTALinksJsonArr.add(ctaLinkJson);
}
}

finalJson.add("ctaLinks", resultCTALinksJsonArr);
}
}

if (jsonObject.has("ctaButtons")) {
JsonArray ctaButtonsJsonArr = jsonObject.get("ctaButtons").getAsJsonArray();

if(ctaButtonsJsonArr!= null) {
JsonArray resultCTAButtonsJsonArr = new JsonArray();
JsonObject ctaLButtonJson;

for (int index = 0; index < ctaButtonsJsonArr.size(); index++) {
ctaLButtonJson = jsonParser.parse(ctaButtonsJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLButtonJson!= null) {
if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}

if (ctaLButtonJson.has("buttonurl")) {
ctaLButtonJson.addProperty("buttonurl", ctaLButtonJson.get("buttonurl").getAsString());
}

if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}
resultCTAButtonsJsonArr.add(ctaLButtonJson);
}
}

finalJson.add("ctaButtons", resultCTAButtonsJsonArr);
}
}
}

 

 

 

Result JSON (finalJson):

 

json.PNG

Avatar

Correct answer by
Level 8

Hi @maheswariv26797 

Please use below code snippet to parse the given JSON string & to construct result JSON object.

 

JsonObject finalJson = new JsonObject();
JsonParser jsonParser = new JsonParser();

JsonObject jsonObject = jsonParser.parse("{\"title\":\"page1\",\"description\":\"this is description\",\"ctaLinks\":[\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en/products\\\",\\\"linktitle\\\":\\\"title1\\\"}\",\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en\\\",\\\"linktitle\\\":\\\"title2\\\"}\"],\"ctaButtons\":[\"{\\\"buttontitle\\\":\\\"button1\\\",\\\"buttonurl\\\":\\\"/content/maximintegrated\\\",\\\"btnStyle\\\":\\\"btn-primary\\\"}\"]}").getAsJsonObject();

if(jsonObject!= null) {
if (jsonObject.has("title")) {
finalJson.addProperty("title", jsonObject.get("title").getAsString());
}

if (jsonObject.has("description")) {
finalJson.addProperty("description", jsonObject.get("description").getAsString());
}

if (jsonObject.has("ctaLinks")) {
JsonArray ctaLinksJsonArr = jsonObject.get("ctaLinks").getAsJsonArray();

if (ctaLinksJsonArr != null) {
JsonArray resultCTALinksJsonArr = new JsonArray();
JsonObject ctaLinkJson;

for (int index = 0; index < ctaLinksJsonArr.size(); index++) {
ctaLinkJson = jsonParser.parse(ctaLinksJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLinkJson!= null) {
if (ctaLinkJson.has("linkpath")) {
ctaLinkJson.addProperty("linkpath", ctaLinkJson.get("linkpath").getAsString());
}

if (ctaLinkJson.has("btnstyle")) {
ctaLinkJson.addProperty("btnstyle", ctaLinkJson.get("btnstyle").getAsString());
}

resultCTALinksJsonArr.add(ctaLinkJson);
}
}

finalJson.add("ctaLinks", resultCTALinksJsonArr);
}
}

if (jsonObject.has("ctaButtons")) {
JsonArray ctaButtonsJsonArr = jsonObject.get("ctaButtons").getAsJsonArray();

if(ctaButtonsJsonArr!= null) {
JsonArray resultCTAButtonsJsonArr = new JsonArray();
JsonObject ctaLButtonJson;

for (int index = 0; index < ctaButtonsJsonArr.size(); index++) {
ctaLButtonJson = jsonParser.parse(ctaButtonsJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLButtonJson!= null) {
if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}

if (ctaLButtonJson.has("buttonurl")) {
ctaLButtonJson.addProperty("buttonurl", ctaLButtonJson.get("buttonurl").getAsString());
}

if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}
resultCTAButtonsJsonArr.add(ctaLButtonJson);
}
}

finalJson.add("ctaButtons", resultCTAButtonsJsonArr);
}
}
}

 

 

 

Result JSON (finalJson):

 

json.PNG

Avatar

Level 2

Hi @Manjunath_K,

 

Thanks! this solution is working.

 

Thanks,

Mahi

Avatar

Level 8

Hi @maheswariv26797 

Please use below code snippet to parse the given JSON string & to construct result JSON object.

 

JsonObject finalJson = new JsonObject();
JsonParser jsonParser = new JsonParser();

JsonObject jsonObject = jsonParser.parse("{\"title\":\"page1\",\"description\":\"this is description\",\"ctaLinks\":[\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en/products\\\",\\\"linktitle\\\":\\\"title1\\\"}\",\"{\\\"linkpath\\\":\\\"/content/maximintegrated/en\\\",\\\"linktitle\\\":\\\"title2\\\"}\"],\"ctaButtons\":[\"{\\\"buttontitle\\\":\\\"button1\\\",\\\"buttonurl\\\":\\\"/content/maximintegrated\\\",\\\"btnStyle\\\":\\\"btn-primary\\\"}\"]}").getAsJsonObject();

if(jsonObject!= null) {
if (jsonObject.has("title")) {
finalJson.addProperty("title", jsonObject.get("title").getAsString());
}

if (jsonObject.has("description")) {
finalJson.addProperty("description", jsonObject.get("description").getAsString());
}

if (jsonObject.has("ctaLinks")) {
JsonArray ctaLinksJsonArr = jsonObject.get("ctaLinks").getAsJsonArray();

if (ctaLinksJsonArr != null) {
JsonArray resultCTALinksJsonArr = new JsonArray();
JsonObject ctaLinkJson;

for (int index = 0; index < ctaLinksJsonArr.size(); index++) {
ctaLinkJson = jsonParser.parse(ctaLinksJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLinkJson!= null) {
if (ctaLinkJson.has("linkpath")) {
ctaLinkJson.addProperty("linkpath", ctaLinkJson.get("linkpath").getAsString());
}

if (ctaLinkJson.has("btnstyle")) {
ctaLinkJson.addProperty("btnstyle", ctaLinkJson.get("btnstyle").getAsString());
}

resultCTALinksJsonArr.add(ctaLinkJson);
}
}

finalJson.add("ctaLinks", resultCTALinksJsonArr);
}
}

if (jsonObject.has("ctaButtons")) {
JsonArray ctaButtonsJsonArr = jsonObject.get("ctaButtons").getAsJsonArray();

if(ctaButtonsJsonArr!= null) {
JsonArray resultCTAButtonsJsonArr = new JsonArray();
JsonObject ctaLButtonJson;

for (int index = 0; index < ctaButtonsJsonArr.size(); index++) {
ctaLButtonJson = jsonParser.parse(ctaButtonsJsonArr.get(index).getAsString()).getAsJsonObject();

if(ctaLButtonJson!= null) {
if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}

if (ctaLButtonJson.has("buttonurl")) {
ctaLButtonJson.addProperty("buttonurl", ctaLButtonJson.get("buttonurl").getAsString());
}

if (ctaLButtonJson.has("buttontitle")) {
ctaLButtonJson.addProperty("buttontitle", ctaLButtonJson.get("buttontitle").getAsString());
}
resultCTAButtonsJsonArr.add(ctaLButtonJson);
}
}

finalJson.add("ctaButtons", resultCTAButtonsJsonArr);
}
}
}

 

 

 

Result JSON (finalJson):

 

json.PNG