Iterate over the JSON object elements in sling model | Community
Skip to main content
Level 2
January 3, 2023
Solved

Iterate over the JSON object elements in sling model

  • January 3, 2023
  • 1 reply
  • 1897 views

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:

  1. Create a new JSON object

  2. 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

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 Siva_Sogalapalli

Please check examples here https://www.geeksforgeeks.org/parse-json-java/ which gives an idea on reading and creating new objects, so that you can write your code based on your json object structure. Hope this helps. 

1 reply

Siva_Sogalapalli
Community Advisor
Siva_SogalapalliCommunity AdvisorAccepted solution
Community Advisor
January 3, 2023

Please check examples here https://www.geeksforgeeks.org/parse-json-java/ which gives an idea on reading and creating new objects, so that you can write your code based on your json object structure. Hope this helps. 

Level 2
January 3, 2023

Thanks @siva_sogalapalli 

Could you please help me, how to get JSON Objects based on prefix words in the key?

 

ex: 

  • Copy the child elements to the new JSON object if name is barType or names starts with foo, but not fooPage
Siva_Sogalapalli
Community Advisor
Community Advisor
January 3, 2023

You can try in different ways based on your requirement & needs.

For example, if the keys are always same and very few, then you can directly get it using key names like

JSONObject jo = (JSONObject) obj;
String barType = (String) jo.get("barType");
String foo = (String) jo.get("foo");

Alternatively, you can also store all the keys in array which you want to read and iterate the array to get all the keys so that you can read the same from json object. Just look for java examples and use the same in sling model should work for you.