Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
format json in textarea
// preview with hilight.js and pretty json format
$('[name="json_data"]').on('input change', function () {
let previewWrapper = $(this).siblings('.preview-wrapper');
const $code = previewWrapper.find('pre > code');
if ($(this).val()) {
try {
var val = JSON.parse($(this).val())
} catch (e) {
$(this).addClass("warning-bordered");
return false;
}
$(this).removeClass("warning-bordered");
previewWrapper.removeClass("hide");
$code.text(JSON.stringify(val, undefined, 2))
hljs.highlightBlock($code[0]);
} else {
previewWrapper.addClass("hide");
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment