Skip to content

Instantly share code, notes, and snippets.

@nguyenit67
Last active April 16, 2022 14:46
Show Gist options
  • Save nguyenit67/930c2a3c2a6f988a473b83537824bfd6 to your computer and use it in GitHub Desktop.
Save nguyenit67/930c2a3c2a6f988a473b83537824bfd6 to your computer and use it in GitHub Desktop.
Postman visualizer for large JSON data in the browser.
const responseJson = pm.response.json();
const resJsonString = JSON.stringify(responseJson);
const template = `
<link rel="stylesheet" href="https://unpkg.com/big-json-viewer/dist/default.css">
<script src="https://unpkg.com/big-json-viewer/dist/browser-api.js"></script>
<style>
body {
height: 100vh;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
font-size: 12.5px;
}
.json-node-root {
flex-grow: 1;
overflow: scroll;
}
.path-show-box {
height: 10vh;
width: 100%;
}
.json-node-root,
.path-show-box {
margin: 0;
padding: 0;
padding-left: 1em;
}
</style>
<input class="path-show-box" type="text" readonly>
<script>
const data = ${resJsonString};
BigJsonViewerDom.fromObject(data).then(viewer => {
const rootNode = viewer.getRootElement();
document.body.prepend(rootNode);
rootNode.openAll(1);
const pathBox = document.querySelector('.path-show-box');
rootNode.addEventListener('copyPath', function(e) {
console.log('copyPath', e.target.jsonNode.path);
const identifierRegex = /[A-Za-z_$]/;
const targetPath = e.target.jsonNode.path.map(propName => {
const firstChar = propName[0];
const dotNotationAble = identifierRegex.test(firstChar);
if (dotNotationAble) {
return \`.\${propName}\`;
} else {
return \`[\${JSON.stringify(propName)}]\`;
}
}).join('');
pathBox.value = targetPath;
});
});
</script>
`;
pm.visualizer.set(template);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment