How do I sort a JSON response

I’d like to sort a JSON response (all of my JSON responses for that matter) alphabetically, including nested objects.

I know this isn’t part of the JSON spec, but I’m using Postman mainly as a way to inspect the data returned from a REST-ish server and I’m a human being, so finding things in a sorted list is way easier than finding things in an unordered list.

Is this possible?

Hi @superdupertango

I’m not sure about sorting alphabetically but you could you .find or .filter to search your response…

Two examples I recently put together;

//Find completes on the first occurance found.
const jsonData = pm.response.json();
pm.test("Result has Mr", () => {
    let result = jsonData.results.find(a => a.name.title === "Mr")
    pm.expect(result.name.title).to.eql("Mr")
});

more info here

//Filter returns all occurances found.
const jsonData = pm.response.json();
pm.test("Use filter to count all US alpha_two_codes", () => {
    let result = jsonData.filter(a => a.alpha_two_code === "US")
    console.log(result.length)
});

More info here

Unfortunately, finding and filtering only gets me part of the way there. There are times where I’m looking at bunch of different data, so really, sorting is what I’m looking for.

Could you give an example, maybe a screenshot showing what you want the end result to look like?

It should be pretty simple, if this is the ordering that is returned from the server and that shows in the Postman response window:

{
    "page": 1,
    "per_page": 3,
    "total": 12,
    "total_pages": 4,
    "data": [
        {
            "id": 1,
            "name": "cerulean",
            "year": 2000,
            "color": "#98B2D1",
            "pantone_value": "15-4020"
        },
        {
            "id": 2,
            "name": "fuchsia rose",
            "year": 2001,
            "color": "#C74375",
            "pantone_value": "17-2031"
        },
        {
            "id": 3,
            "name": "true red",
            "year": 2002,
            "color": "#BF1932",
            "pantone_value": "19-1664"
        }
    ],
    "support": {
        "url": "https://reqres.in/#support-heading",
        "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
    }
}

the sorted version would look like this:

{
    "data": [
        {
            "color": "#98B2D1",
            "id": 1,
            "name": "cerulean",
            "pantone_value": "15-4020",
            "year": 2000
        },
        {
            "color": "#C74375",
            "id": 2,
            "name": "fuchsia rose",
            "pantone_value": "17-2031",
            "year": 2001
        },
        {
            "color": "#BF1932",
            "id": 3,
            "name": "true red",
            "pantone_value": "19-1664",
            "year": 2002
        }
    ],
    "page": 1,
    "per_page": 3,
    "support": {
        "text": "To keep ReqRes free, contributions towards server costs are appreciated!",
        "url": "https://reqres.in/#support-heading"
    },
    "total": 12,
    "total_pages": 4
}

Thanks!

I don’t know if this is possible in Postman directly, but here is a site that does exactly what you showed in the screenshots above;

This guy also has the code in GitHub, maybe someone with more coding knowledge than me could plug it into Postman somehow? :man_shrugging:

1 Like

This doesn’t sort the actual response, but it is a quick and dirty workaround for Test visualizer. For JSON response of form:

{ "id": 1, "name": "Joe" }, { "id": 2, "name": "Amy" }, { "id": 3, "name": "Ed" }

In the Test window:

// sort helper
function compareBy(a, b) {
  if (a.name < b.name) { return -1; }
  if (a.name > b.name) { return  1; }
  return 0;
}

var template = ` ... `;

// set visualizer
pm.visualizer.set(template, {
    response: pm.response.json().sort(compareBy)
});

It sounded like the original poster wanted to sort the elements on key name.

Here is a Stack Overflow conversation that details how to do that for a single object.

sorting - Sort JavaScript object by key - Stack Overflow

If you want to sort the array on a particular key, then the following topic explains the options.

javascript - Sort objects in an array alphabetically on one property of the array - Stack Overflow

It sounds like the answer to the original question would be a combination of these techniques.

There isn’t a straight forward way to do this, as it depends on how complex the response\json is. Arrays within arrays, objects within objects, and objects stored as strings. I don’t think you will get a single function that can handle all of these elements without it being a node.js application.