Open chbussler opened 7 years ago
@chbussler Thanks for posting this issue 👍 . Support for a custom JSON viewer that would allow sorting, is part of the roadmap.
I want this .Sorted key should make effective work. Like: { "a": 12, "abc": "x", "c" : { "a" : 15 } }
👍 I agree, this would help a lot with reading long and nested JSON output from REST APIs.
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.
👍
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
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)
});
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.