Sort 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: