Skip to main content
The Deno 2 Release Candidate is here
Learn more

JSON Tree

Json Tree 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, hideFunctions?: boolean })

Where:

  • obj: json Object
  • showValues: Whether or not to show the object values in the tree
  • hideFunctions : Whether or not to show functions in the tree

Screenshots

With Values

image

Without Values

image

Acknowledgement

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