postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.84k stars 839 forks source link

Sort properties in JSON result body on demand alphabetically (e.g., sort button) #2751

Open chbussler opened 7 years ago

chbussler commented 7 years ago

The JSON results I am working with a rather large and often it is cumbersome to use the search function to search for a specific property, esp. if there are several that have the same prefix in the name.

It would help the efficiency a lot if the properties in a body could be sorted alphabetically (and recursively) on demand. E.g. unsorted:

{"b":1, "a": 2}

Sorted:

{"a":2, "b":1}

Pretty print then would give me a fast way to find properties by searching with the eye, rather than the search function.

vegetableman commented 7 years ago

@chbussler Thanks for posting this issue 👍 . Support for a custom JSON viewer that would allow sorting, is part of the roadmap.

unknown-undefined commented 6 years ago

I want this .Sorted key should make effective work. Like: { "a": 12, "abc": "x", "c" : { "a" : 15 } }

andrewrproper commented 5 years ago

👍 I agree, this would help a lot with reading long and nested JSON output from REST APIs.

jrupp commented 5 years ago

This would vastly improve my efficiency when using this with large amount of return data. Also, there is a bug report (#5659) that says some amount of sorting is happening automatically, just mentioning that here in case whatever bug is happening can actually help along this feature.

alskaer commented 4 years ago

👍

gcsizmadia commented 2 years ago

Is there any progress regarding this? Currently I copy the JSON response to Visual Studio to get its properties sorted. It would be really great if sorting properties was available inside Postman.

I also tried the code below in the Tests tab and now I can see the sorted JSON object in the Postman Console window:

function sortObject(unordered, sortArrays = false) {
    if (!unordered || typeof unordered !== 'object') {
        return unordered;
    }

    if (Array.isArray(unordered)) {
        const newArr = unordered.map((item) => sortObject(item, sortArrays));
        if (sortArrays) {
            newArr.sort();
        }
        return newArr;
    }

    const ordered = {};
    Object.keys(unordered)
        .sort()
        .forEach((key) => {
            ordered[key] = sortObject(unordered[key], sortArrays);
        });
    return ordered;
}

const sortedJson = sortObject(pm.response.json(), true);
console.log(sortedJson);

Credit: konsta.2106 Reference: original code snippet

DaveSkender commented 9 months ago

Riffing off of @gcsizmadia, I was able to get a quick and dirty workaround here for Test visualizer. For JSON response of form:

{ "id": 1, "name": "Joe" }, { "id": 2, "name": "Amy" }, { "id": 3, "name": "Ed" }
// 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)
});