Skip to content

Instantly share code, notes, and snippets.

@leesei
Created April 29, 2024 01:53
Show Gist options
  • Save leesei/cc68abb91fb536c4b443f1f0be387a8a to your computer and use it in GitHub Desktop.
Save leesei/cc68abb91fb536c4b443f1f0be387a8a to your computer and use it in GitHub Desktop.
Convert Kibana exported dashboard NDJSON to JSON for readability
#!/usr/bin/env bun
const readline = require("node:readline");
const fs = require("node:fs");
const rl = readline.createInterface({
input: fs.createReadStream("export.ndjson"),
});
rl.on("line", (line) => {
o = JSON.parse(line);
walk_json(o);
console.log(JSON.stringify(o, null, 2));
});
json_keys = new Set(["visState"]);
function walk_json(object) {
Object.entries(object).forEach(([key, value]) => {
// console.log(key, typeof value, value);
if (typeof value === "object") {
walk_json(value);
} else if (
typeof value === "string" &&
(key.endsWith("JSON") || key.startsWith("field") || json_keys.has(key))
) {
try {
object[key] = JSON.parse(value);
} catch (err) {
console.log("Failed to parse JSON", err);
process.exit(1);
}
}
});
}
#!/usr/bin/env python3
import json
json_keys = set(["visState"])
def walk_json(o):
for key in o:
# print(key, type(o[key]))
t = type(o[key])
if t == dict:
walk_json(o[key])
elif t == str and (
key.endswith("JSON") or key.startswith("field") or key in json_keys
):
o[key] = json.loads(o[key])
with open("export.ndjson") as f:
for line in f:
o = json.loads(line)
walk_json(o)
print(json.dumps(o, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment