Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Created September 14, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvdsluijs/9f4f82bd8e71848733d482ef1916bebf to your computer and use it in GitHub Desktop.
Save tvdsluijs/9f4f82bd8e71848733d482ef1916bebf to your computer and use it in GitHub Desktop.
Enable postman json tree view
// https://itheo.tech/supercharge-your-postman-experience-with-custom-json-visualization?showSharer=true
const isJsonString = (str) => {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
const getResponseJson = (responseText) => {
return isJsonString(responseText) ? pm.response.json() : { '_PAYLOAD': responseText };
};
const responseText = pm.response.text();
const responseJson = getResponseJson(responseText);
const resJsonString = JSON.stringify(responseJson);
const template = `
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/jsoneditor@9.10.2/dist/jsoneditor.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/jsoneditor@9.10.2/dist/jsoneditor.min.js"></script>
<style>
table {
margin-bottom: 0;
}
table, table tr, table td {
border: none !important;
}
</style>
<div id="jsoneditor" style="width: 100%; height: 100%;"></div>
<script>
const container = document.getElementById("jsoneditor");
const options = {};
const editor = new JSONEditor(container, options);
editor.set(${resJsonString});
const updatedJson = editor.get();
</script>
`;
pm.visualizer.set(template);
@tvdsluijs
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment