Sort JSON response

While working with JSON, you might have come across scenarios where you want to sort the data before binding it to controls or using it for some other purpose. This use case demonstrates how to sort JSON data using custom JS Code.

This article makes use of JSON data received from an API.

JSON Data (Raw Response)

{
  "result": {
    "rows": [
      {
        "ID": 18,
        "FirstName": "Tomasa",
        "LastName": "Hermann",
        "Address": "751 Koss Forest",
        "Email": "tomasa.hermann41@example.com",
        "Designation": "Intern"
      },
      {
        "ID": 24,
        "FirstName": "Cedrick",
        "LastName": "Schmeler",
        "Address": "695 Rau Passage",
        "Email": "cedrick89@example.com",
        "Designation": "Workspace Admin"
      },
      {
        "ID": 36,
        "FirstName": "Timmy",
        "LastName": "Schamberger",
        "Address": "24593 Bayer Oval",
        "Email": "timmy_schamberger@example.com",
        "Designation": "Workspace Manager"
      },
      {
        "ID": 1,
        "FirstName": "Carlie",
        "LastName": "Schmeler",
        "Address": "2375 Allen Flats",
        "Email": "carlie43@example.com",
        "Designation": "Automation Tester"
      },
      {
        "ID": 2,
        "FirstName": "Katrina",
        "LastName": "Mosciski",
        "Address": "2570 Toy Lights",
        "Email": "katrina94@example.com",
        "Designation": "Software Developer"
      },
      {
        "ID": 3,
        "FirstName": "Lurline",
        "LastName": "Powlowski",
        "Address": "551 Morar Expressway",
        "Email": "lurline90@example.com",
        "Designation": "Market Intern"
      },
      {
        "ID": 11,
        "FirstName": "Micah",
        "LastName": "Mosciski",
        "Address": "5184 Keely Trafficway",
        "Email": "micah.mosciski65@example.com",
        "Designation": "Intern"
      }
    ]
  }
}

 
In the connectors, transform response section, we can write JS Code to perform sort operation on JSON data based on a key attribute.

Code

function sortJSON(arr, key, asc=true) {
  return arr.sort((a, b) => {
    let x = a[key];
    let y = b[key];
    if (asc) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }
    else { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }
  });
}
output = sortJSON(data.result.rows, "ID", false);

Code Explanation

The code contains an inner function “sortJSON” which is responsible for the actual sorting based on the key attribute supplied at the time of invoking the function. The return value of this function is a JSON object stored in the output variable.

The sortJSON function accepts three parameters, two are mandatory while one is optional. The first parameter accepts a JSON Object that needs to be sorted. The second parameter accepts a string, the key attribute upon which the sorting needs to happen. The third and final parameter accepts a boolean value, and is used to toggle the sorting order. If this optional parameter is left blank, it defaults to true which will sort the JSON in ascending order and if the value is false, it will sort in descending order.

JSON Data (Transformed Response)

{
  "result": {
    "rows": [
      {
        "ID": 36,
        "FirstName": "Timmy",
        "LastName": "Schamberger",
        "Address": "24593 Bayer Oval",
        "Email": "timmy_schamberger@example.com",
        "Designation": "Workspace Manager"
      },
      {
        "ID": 24,
        "FirstName": "Cedrick",
        "LastName": "Schmeler",
        "Address": "695 Rau Passage",
        "Email": "cedrick89@example.com",
        "Designation": "Workspace Admin"
      },
      {
        "ID": 18,
        "FirstName": "Tomasa",
        "LastName": "Hermann",
        "Address": "751 Koss Forest",
        "Email": "tomasa.hermann41@example.com",
        "Designation": "Intern"
      },
      {
        "ID": 11,
        "FirstName": "Micah",
        "LastName": "Mosciski",
        "Address": "5184 Keely Trafficway",
        "Email": "micah.mosciski65@example.com",
        "Designation": "Intern"
      },
      {
        "ID": 3,
        "FirstName": "Lurline",
        "LastName": "Powlowski",
        "Address": "551 Morar Expressway",
        "Email": "lurline90@example.com",
        "Designation": "Market Intern"
      },
      {
        "ID": 2,
        "FirstName": "Katrina",
        "LastName": "Mosciski",
        "Address": "2570 Toy Lights",
        "Email": "katrina94@example.com",
        "Designation": "Software Developer"
      },
      {
        "ID": 1,
        "FirstName": "Carlie",
        "LastName": "Schmeler",
        "Address": "2375 Allen Flats",
        "Email": "carlie43@example.com",
        "Designation": "Automation Tester"
      }
    ]
  }
}

Initial Data (Unsorted)

Sorted Data (ID as key attribute in descending order)