Json Values in nested multifield | Community
Skip to main content
New Participant
September 29, 2020
Solved

Json Values in nested multifield

  • September 29, 2020
  • 5 replies
  • 1666 views

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

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 Manjunath_K

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):

 

5 replies

Manjunath_K
New Participant
September 30, 2020

@maheswariv26797 

You’re welcome 🙂

Manjunath_K
New Participant
September 30, 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):

 

New Participant
September 29, 2020

Hi @manjunath_k,

 

Thanks! this solution is working.

 

Thanks,

Mahi

Manjunath_K
Manjunath_KAccepted solution
New Participant
September 29, 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):

 

Manjunath_K
New Participant
September 29, 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):