Skip to content

Instantly share code, notes, and snippets.

@thc282
Last active February 26, 2026 04:10
Show Gist options
  • Select an option

  • Save thc282/81565368e19761c9247343b5a4704e1d to your computer and use it in GitHub Desktop.

Select an option

Save thc282/81565368e19761c9247343b5a4704e1d to your computer and use it in GitHub Desktop.
Check JSON validation
(function() {
const text = document.body.textContent; /* define text properly */
try {
JSON.parse(text);
alert("✅ JSON is valid!");
} catch (error) {
console.error("❌ Invalid JSON: " + error.message);
/* Extract position from error message */
const match = error.message.match(/position (\d+)/);
if (match) {
const pos = parseInt(match[1], 10);
/* Compute line and column */
const before = text.substring(0, pos);
const lines = before.split("\n");
const lineNumber = lines.length;
const columnNumber = lines[lines.length - 1].length + 1;
console.error(`Error at line ${lineNumber}, column ${columnNumber}`);
/* Show JSON with line numbers and highlight the error line */
const container = document.createElement("pre");
container.style.whiteSpace = "pre-wrap";
container.style.fontFamily = "monospace";
const allLines = text.split("\n");
allLines.forEach((line, i) => {
const div = document.createElement("div");
div.textContent = (i + 1) + ": " + line;
if (i + 1 === lineNumber) {
div.style.background = "yellow"; /* highlight error line */
}
container.appendChild(div);
});
document.body.innerHTML = ""; /* clear old content */
document.body.appendChild(container);
/* Scroll to the highlighted line */
const highlighted = container.querySelector("div[style*='yellow']");
if (highlighted) {
highlighted.scrollIntoView({ behavior: "smooth", block: "center" });
}
}
}
})();

In Dev tools

  1. Go to source
  2. Add snippet

Bookmark

  1. Manually add javascript:{code above}
  2. Click to use
Example:
javascript:(function(){const text=document.body.textContent;..........}}}})();
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment