0

I'm not sure whether this is possible or not. Say I got a JSON array similar to the one below

   {    
    "hitsPerPage": 30,
    "hitList": {
        "Nodes": [
       {
            "Path": "/content/desktop/gt/en/personal/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/save-and-invest/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }, {
            "Path": "/content/desktop/gt/en/investment-accounts/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/tools-and-guidance/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",           
        }]
    }
}

Is it possible to re-order the order in which the Nodes array of objects are displayed, something like

"Nodes": [
        {
            "Path": "/content/desktop/gt/en/investment-accounts/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/save-and-invest/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }, 
            "Path": "/content/desktop/gt/en/tools-and-guidance/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",           
        },{
            "Path": "/content/desktop/gt/en/personal/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }]

Any sample code in Java if this is possible

Zahori
  • 446
  • 1
  • 4
  • 16
mr_j
  • 125
  • 1
  • 15
  • the order of elements in JSON arrays is preserved. look http://www.rfc-editor.org/rfc/rfc7159.txt – surendrapanday Jun 05 '18 at 09:53
  • Once you have the array in your program you are free to do to it whatever you like (reorder, add elements, remove elements, ...). – Henry Jun 05 '18 at 09:56
  • may be this will help. https://stackoverflow.com/questions/7214293/is-the-order-of-elements-in-a-json-list-preserved/7214312?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – surendrapanday Jun 05 '18 at 09:56

1 Answers1

0

Easiest way would just be to extract the data and re-write the json in the desired format. Google's Gson library is excellent for extracting the data into a java class and writing to json. Or write to Json manually with JsonObject/array/etc. Think about whether restructuring the json is necessary though, hard to see why it would be overly important

  • The nodes array is whats shown on the browser. The client wants to be able to rearrange the array so some results are on top of others e.g. investment accounts shows before tools and guidance – mr_j Jun 05 '18 at 10:01