Iterate over the JSON object elements in sling model
I have the following JSON Object
import org.json.JSONObject;
JSONObject jsonObject = new JSONObject("jsonString")
The output of the above jsonObject follows:
{
"elements": {
"barType": "bar",
"website": "website",
"endDate": "end",
"fooText": "text",
"fooDisclaimer": "Disclaimer",
"type": "type",
"fooPage": {
"string": "string",
"value": [
{
"number": "2",
"pagePath": "/foo/bar/home/selector/extension"
},
{
"number": "5",
"pagePath": "/foo/bar"
}
]
},
"fooTitle": "Title",
"locations": "locations"
},
"items": "items"
}
Now I want to perform following steps:
-
Create a new JSON object
-
iterate over the "elements" node in the above json object (jsonObject)
- Copy the child elements to the new JSON object if name is barType or names starts with foo, but not fooPage
- If name = fooPage:
- iterate over the values to find one where pagePath = currentPageURL add a new property called fooPriority to the new JSON object with the value from the number
Note: currentPageURL = "/foo/bar"
The expected output of new JSON object would be:
{
"barType": "bar",
"fooText": "text",
"fooDisclaimer": "Disclaimer",
"fooPriority": 5,
"fooTitle": "Title",
"locations": "locations"
}
Please help me with the implementation of Java code by using org.json.JSONObject;
Thanks in advance
