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 🙏

© 2024 – Pkg Stats / Ryan Hefner

big-json-viewer

v0.1.7

Published

JavaScript Library to view big JSON structures.

Downloads

1,416

Readme

Big JSON Viewer

npm Travis Codecov code style: prettier

A JavaScript library that enables efficient working with large JSON data in the browser.

The JSON data is held as ArrayBuffer and only parsed for structural information.

Information about the top level nodes is provided. Pagination enabled browsing of arrays and objects.

No dependencies, works directly on the DOM API. Runs in any modern browser and IE11.

View the Demo

Usage

npm install big-json-viewer

Example usage

test.ts

import { BigJsonViewerDom } from 'big-json-viewer';

BigJsonViewerDom.fromData(JSON.stringify({ test: 23 })).then(viewer => {
    const node = viewer.getRootElement();
    document.body.appendChild(node);
    node.openAll(1);
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
  <link rel="stylesheet" href="./node_modules/big-json-viewer/styles/default.css">
</head>
<body>
  <script src="src/test.ts"></script>
</body>
</html>

Example run with parcel (npm install -D parcel-bundler);

parcel index.html

More examples

See examples/basic/index.html for plain javascript example.

See docs-src/demo.ts for a more advanced example.

Getting started

You can use the following static method to get a new viewer instance:

import { BigJsonViewerDom, BigJsonViewerOptions } from 'big-json-viewer';
BigJsonViewerDom.fromData(data: ArrayBuffer | string, options?: BigJsonViewerOptions): Promise<BigJsonViewerDom>

It returns a BigJsonViewerDom instance. Call getRootElement() on it to get a JsonNodeElement, that is an HTMLDivElement with some extras. You can insert it anywhere in your DOM.

Options

When calling fromData, you can provide an object matching the interface BigJsonViewerOptions.

Example:

{
  objectNodesLimit: 50, // how many properties of an object should be shows before it gets paginatated with a pagination size of 50
  arrayNodesLimit: 50, // same as objectNodesLimit, but with array elements
  labelAsPath: false // if true the label for every node will show the full path to the element
}

API

BigJsonViewerDom static methods

fromData(data: ArrayBuffer | string, options?: BigJsonViewerOptions): Promise<BigJsonViewerDom>

Initilizes a new viewer with JSON encoded data

fromObject(data: string | object | null | number | boolean, options?: BigJsonViewerOptions): Promise<BigJsonViewerDom>

Initializes a new viewer with JavaScript data

BigJsonViewerDom methods

getRootElement()

Returns the JsonNodeElement that can be appended to the DOM.

destroy()

Call this to free resources. It will terminate any by the instance started worker.

openBySearch(pattern: RegExp, openLimit?: number, searchArea?: TreeSearchAreaOption): TreeSearchCursor;

Searches the tree by the specified pattern and searchArea. Returns a TreeSearchCursor, which contains all matches and methods to jump the focus between the matches.

  • openLimit is 1 by default. But can be Infinity or any number.
  • searchArea describes where the pattern should be searched. Has the following options:
    • 'all' search in keys and values (default)
    • 'keys' search only in keys
    • 'values' search only in values

JsonNodeElement methods

openNode()

Opens the node in case it is an openable node. No event is fired.

closeNode()

Closes the node in case it is open. No event is fired.

toggleNode()

Toggles the open state of the node. Either opens or closes it. No event is fired.

openPath(path: string[]): JsonNodeElement

Opens the specified path and returns the opened node, in case it was found.

openAll(maxDepth?: number, paginated?: PaginatedOption): number

Opens all nodes until the defined depth. Returns the number of opened nodes.

  • maxDepth is Infinity by default
  • paginated is a string of the following options
    • 'first' open only the first pagination stub (default)
    • 'all' open all pagination stubs
    • 'none' open no pagination stubs

getOpenPaths(withStubs?: boolean): string[][]

Returns a list of opened paths. withStubs is true by default. It makes sure, that paginated stubs that are opened are considered.

When you have a limit of 50 nodes and you open the second stub [50 ... 99], a path it retuned that contains the name of the first node in the stub.

JsonNodeElement Events

The following events are being fired on the visible DOM elements. The events bubble up, so you just need a listener to your root element.

openNode

Fires when a node is being opened by the user directly with a click. The target is a JsonNodeElement.

Example logs the opened path:

rootNode.addEventListener('openNode', function(e) {
    console.log('openNode', e.target.jsonNode.path);
});

closeNode

Fires when a node is being closed. The target is a JsonNodeElement.

openedNodes

Fires when multiple nodes have been opened. Target is the top level JsonNodeElement that was used to trigger the action. E.g. when the user clicks the Expand all link.

openStub

Fires when a pagination stub is being opened directly by the user with a click. The target is a JsonNodesStubElement.

closeStub

Fires when a pagination stub is being closed. The target is a JsonNodesStubElement.

copyPath

Fires when the user clicks on the Copy Path link of a node.

Contributing

Anyone is welcome to contribute.

If something has changed that affects the Docs/Demo page, run:

npm run build-docs

Future TODOs

  • Improve display of large strings.
  • Support JSON Schema. If provided show meta information from the schema definition.

License

MIT