npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vdonec/json-view

v2.0.2

Published

This is a javascript library for displaying json data into a DOM

Readme

json-view

A small JavaScript library to display JSON data as an expandable tree in the DOM.

✨ Zero runtime dependencies — Fully self-contained library

Live Demo

Try the interactive demo: https://vdonec.github.io/json-view/

The demo lets you:

  • Load example JSON or generate large test data
  • Toggle rendering options (defaultExpanded, showValueType, virtualize, overscanRows)
  • Expand/collapse nodes and inspect statistics
  • Test virtualization performance with thousands of nodes

Quick start (local demo)

npm install
npm run dev

Then open http://localhost:5173/.

The demo page is index.html, demo logic is in src/main.js.

Build package

npm run build

Build output is written to dist/:

  • dist/jsonview.js — ES module
  • dist/jsonview.umd.cjs — UMD (CommonJS)
  • dist/jsonview.css — styles
  • dist/jsonview.d.ts — TypeScript declarations

Install from npm

npm install @vdonec/json-view

Usage

import "@vdonec/json-view/style.css";
import jsonview from "@vdonec/json-view";

const data = {
  name: "json-view",
  version: "1.x",
  features: ["render", "expand", "collapse", "virtualize"],
};

const root = document.querySelector("#root");

// Basic render
let tree = jsonview.renderJSON(data, root);

// Re-render into the same target: destroy previous tree first
jsonview.destroy(tree);
tree = jsonview.renderJSON(data, root, { defaultExpanded: true });

jsonview.destroy(tree);
tree = jsonview.renderJSON(data, root, { defaultExpanded: 1 });

jsonview.destroy(tree);
tree = jsonview.renderJSON(data, root, { showValueType: true });

jsonview.destroy(tree);
tree = jsonview.renderJSON(data, root, {
  defaultExpanded: true,
  virtualize: true,
  showScrollPath: true,
  overscanRows: 8,
  viewportElement: root, // external scroll container (e.g. #root with overflow:auto)
});

// Expand / collapse programmatically
jsonview.expand(tree);
jsonview.collapse(tree);

// Toggle a single node
jsonview.toggleNode(tree);

// Traverse all loaded nodes
jsonview.traverse(tree, (node) => console.log(node.key, node.value));

// Clean up listeners and remove from DOM
jsonview.destroy(tree);

Options

defaultExpandedboolean | number

Controls how deep nodes are expanded on initial render.

| Value | Behaviour | |---|---| | false / omitted | all nodes collapsed | | true | all nodes expanded | | 0 | only root node expanded | | 1 | root + first-level children expanded | | N | nodes with depth <= N expanded (root depth is 0) |

Non-integer or negative values are treated as collapsed-by-default.

showValueTypeboolean (default: false)

When true, adds a type label (e.g. string, number, object, array) before each rendered node value.

Virtualization options

Virtualization keeps only the rows currently visible in the scroll container in the DOM. This is essential for large JSON payloads with thousands of nodes. In virtualized mode, the container also shows a sticky breadcrumb at the top with the path of the current top visible row. Segments are visually separated with a caret icon, and if the top visible row is a leaf node, its last segment is omitted (for example: object > data > items > 0 > payload becomes object > data > items > 0).

| Option | Type | Default | Description | |---|---|---|---| | virtualize | boolean | false | Enable virtualized rendering | | showScrollPath | boolean | true | Show/hide sticky breadcrumb path for the current top visible row | | overscanRows | number | 8 | Extra rows rendered above and below the visible area for smoother scrolling | | viewportElement | HTMLElement | .json-container | The scroll container used to calculate visible rows |

How to set up the scroll container:

By default the library listens for scroll on its own .json-container. If your layout scrolls on a parent element (e.g. #root with overflow: auto and max-height), pass that element via viewportElement:

<div id="root" style="max-height: 80vh; overflow: auto;"></div>
const root = document.querySelector("#root");
jsonview.renderJSON(data, root, {
  virtualize: true,
  showScrollPath: true,
  viewportElement: root,
});

The library automatically handles viewport resize (via ResizeObserver) so the visible window is recalculated after programmatic expand/collapse without requiring a manual scroll.

API

create(jsonData, options)

Parses jsonData (JSON string or any JSON-compatible value) and returns a tree object. Does not touch the DOM.

| Option | Type | Description | |---|---|---| | defaultExpanded | boolean \| number | See Options | | showValueType | boolean | See Options |

render(tree, targetElement, options)

Mounts an existing tree object into targetElement.

targetElement can contain only one mounted json-view tree at a time. Call destroy(tree) before rendering another tree into the same target.

| Option | Type | Description | |---|---|---| | virtualize | boolean | Enable virtualized rendering | | showScrollPath | boolean | Show/hide sticky breadcrumb path in virtualized mode | | overscanRows | number | Extra rows above/below viewport | | viewportElement | HTMLElement | External scroll container |

renderJSON(jsonData, targetElement, options)

Shortcut for create + render in one call. Accepts all options from both.

targetElement can contain only one mounted json-view tree at a time. For re-render into the same target, call destroy(tree) first.

expand(node)

Recursively expands node and all its descendants.

collapse(node)

Recursively collapses node and all its descendants.

toggleNode(node)

Toggles a single node between expanded and collapsed.

traverse(node, callback)

Depth-first traversal of all currently-loaded nodes in the subtree. callback receives each node object.

destroy(tree)

Removes all event listeners (scroll, resize, ResizeObserver) and detaches the rendered container from the DOM.

Security

This library prioritizes security:

  • Zero runtime dependencies — No external dependencies to audit
  • Minimal footprint — Only what's needed for JSON rendering
  • Safe text rendering — Escapes untrusted JSON-derived text before injecting UI markup
  • Regression coveragetest/xss-regression.test.js ensures untrusted keys/values render as text, never as executable HTML
  • No code execution — Simply renders data, never evaluates it

For security policies and reporting vulnerabilities, see SECURITY.md.

Project structure

json-view/
  index.html               # local demo page
  public/
    example2.json          # demo JSON payload
  src/
    json-view.js           # library core
    main.js                # demo bootstrap
    style.css              # library styles
    utils/                 # helper modules
  test/
    xss-regression.test.js # security regression tests
    virtualization.test.js # virtualization tests
  dist/                    # build output (generated)

Scripts

npm run dev      # start Vite dev server
npm run build    # build library to dist/
npm run preview  # preview built output
npm run test     # build + run automated tests