Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Json Values in nested multifield

maheswariv26797
Level 2
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
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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
Manjunath_K
Community Advisor
Community Advisor

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

Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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

maheswariv26797
Level 2
Level 2

Hi @Manjunath_K,

 

Thanks! this solution is working.

 

Thanks,

Mahi

Manjunath_K
Community Advisor
Community Advisor

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