Json Values in nested multifield

maheswariv26797

29-09-2020

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

Accepted Solutions (1)

Accepted Solutions (1)

Manjunathh

29-09-2020

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

Answers (2)

Answers (2)

Manjunathh

29-09-2020

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

Manjunathh

29-09-2020

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