Expand my Community achievements bar.

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

Avatar

Level 8

@maheswariv26797 

You’re welcome

The following has evaluated to null or missing: ==> liql("SELECT id, subject, body, depth, post_time, author.login, author.id, author.rank, metrics.views FROM messages WHERE topic.id = '${topicId}' AND is_solution = true").data.items[0] [in template "analytics-container" at line 82, column 31] ---- Tip: It's the final [] step that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign acceptedAnswer = liql("SELECT... [in template "analytics-container" at line 82, column 5] ----