Created
September 14, 2023 11:32
-
-
Save tvdsluijs/9f4f82bd8e71848733d482ef1916bebf to your computer and use it in GitHub Desktop.
Enable postman json tree view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved code from https://github.com/zhouhao/zhouhao/blob/main/code/postman-tree-view.js