Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

jsontree

jsontree converts a JS object into a nice, visible depth-indented tree for console printing. The structure generated is similar to what you get by running the tree command on Unixy platforms.

{
    oranges: {
        'mandarin': {                                          β”œβ”€ oranges
            clementine: null,                                  β”‚  └─ mandarin
            tangerine: 'so cheap and juicy!'        -=>        β”‚     β”œβ”€ clementine
        }                                                      β”‚     └─ tangerine: so cheap and juicy!
    },                                                         └─ apples
    apples: {                                                     β”œβ”€ gala
        'gala': null,                                             └─ pink lady
        'pink lady': null
    }
}

It also works well with larger nested hierarchies such as file system directory trees.

Quick start

Programmatic

import { jsonTree } from "https://deno.land/x/jsontree@{VERSION}/mod.ts";
console.log(
  jsonTree({
    apples: "gala", //  β”œβ”€ apples: gala
    oranges: "mandarin", //  └─ oranges: mandarin
  }),
);

CLI

Alternatively, you can use it directly from the CLI:

Read JSON from local directory

deno run --allow-read https://deno.land/x/jsontree@1.0.0/cli.ts path sample.json

Read JSON from server

deno run --allow-net https://deno.land/x/jsontree@1.0.0/cli.ts fetch https://jsonplaceholder.typicode.com/users

Install globally

deno install --allow-net --allow-read -n jsontree https://deno.land/x/jsontree@1.0.0/cli.ts

Then, the binary is available to run:

jsontree

Configuration

Required permissions:

  1. --allow-net (if using cli fetch option)
  2. --allow-read (if using cli path option or programmatic API)

Usage

jsonTree(obj, { showValues?: boolean })

Where:

  • obj: json object
  • showValues: whether or not to show the object values in the tree

Screenshots

With Values

image

Without Values

image

Acknowledgement

This is a fork of the original satty1987/json_tree module.