Skip to content

Instantly share code, notes, and snippets.

@ndc
Created July 14, 2024 03:12
Show Gist options
  • Save ndc/65237ed723fd3e76f86c74bc43a92948 to your computer and use it in GitHub Desktop.
Save ndc/65237ed723fd3e76f86c74bc43a92948 to your computer and use it in GitHub Desktop.
Read JSON from file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read JSON from file</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" id="theform">
<input type="file" id="fileinput">
<button type="submit">Read</button>
</form>
<textarea id="theoutput" cols="120" rows="30"></textarea>
<script type="module">
document.getElementById("theform").addEventListener("submit", readjson);
function readjson(ev) {
ev.preventDefault();
const fileinput = document.getElementById("fileinput");
const reader = new FileReader();
reader.addEventListener("load", manipulateJSON);
reader.readAsText(fileinput.files[0]);
}
function manipulateJSON(ev) {
const thejson = JSON.parse(ev.target.result);
// put breakpoint here to manipulate the JSON in the debugger
const theoutput = thejson.data.list.members_timeline.timeline.instructions[1].entries
.filter(e => !!e.content.itemContent)
.map(e => e.content.itemContent.user_results.result.legacy.screen_name)
.join("\n");
document.getElementById("theoutput").value = theoutput;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment