0
0
Fork 0
deno_monorepo/bin/jpath.ts

28 lines
628 B
TypeScript

function traverse(obj: unknown, path: string = "") {
switch (typeof obj) {
case "object":
if (obj !== null) {
for (const [k, v] of Object.entries(obj)) {
traverse(v, Array.isArray(obj) ? `${path}[${k}]` : `${path}."${k}"`);
}
break;
}
default:
console.log(`${path}: ${obj}`);
}
}
if (import.meta.main) {
if (Deno.args.length !== 0) {
console.error("Usage: jpath");
Deno.exit(1);
}
const filename = Deno.args[0];
const json_text = new TextDecoder().decode(Deno.readAllSync(Deno.stdin));
const json = JSON.parse(json_text);
traverse(json);
}