• Stars
    star
    412
  • Rank 105,024 (Top 3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render JSON into collapsible HTML

Renderjson

Render JSON into collapsible, themeable HTML. This library aims to be very simple with few options and no external dependencies. It's aimed at debugging but you can use it wherever it is useful.

The code renders the JSON lazily, only building the HTML when the user reveals the JSON by clicking the disclosure icons. This makes it extremely fast to do the initial render of huge JSON objects, since the only thing that renders initially is a single disclosure icon.

Live Example

A live example can be found here.

Example

<div id="test"></div>
<script type="text/javascript" src="renderjson.js"></script>
<script>
    document.getElementById("test").appendChild(
        renderjson({ hello: [1,2,3,4], there: { a:1, b:2, c:["hello", null] } })
    );
</script>

Usage

The module exports one entry point, the renderjson() function. It takes in the JSON you want to render as a single argument and returns an HTML element.

Options

There are a couple functions to call to customize the output:

renderjson.set_icons('+', '-');

Call set_icons() to set the disclosure icons to something other than "⊕" and "⊖".

renderjson.set_show_to_level(level);

Call set_show_to_level() to show different amounts of the JSON by default. The default is 0, and 1 is a popular choice. As a special case, if level is the string "all" then all the JSON will be shown by default. This, of course, removes the benefit of the lazy rendering, so it may be slow with large JSON objects.

renderjson.set_max_string_length(length);

Strings will be truncated and made expandable if they are longer than length. As a special case, if length is the string "none" then there will be no truncation. The default is "none".

renderjson.set_sort_objects(sort_bool);

Sort objects by key (default: false)

renderjson.set_replacer(replacer_function)
renderjson.set_property_list(property_list)

These are the equivalent of the JSON.stringify() replacer parameter. Mozilla's documentation has a good description of how this parameter works. See test.html for an example of what these can do.

renderjson.set_collapse_msg(collapse_function);

Accepts a function (len:number):string => {} where len is the length of the object collapsed. Function should return the message displayed when a object is collapsed. The default message is "X items".

These functions are chainable so you may do:

renderjson.set_icons('+', '-')
          .set_show_to_level(2)
        ({ hello: [1,2,3,4], there: { a:1, b:2, c:["hello", null] } })

Theming

The HTML output uses a number of classes so that you can theme it the way you'd like:

.disclosure    ("⊕", "⊖")
.syntax        (",", ":", "{", "}", "[", "]")
.string        (includes quotes)
.number
.boolean
.key           (object key)
.keyword       ("null", "undefined")
.object.syntax ("{", "}")
.array.syntax  ("[", "]")

Copyright and License

License: ISC

Copyright © 2013-2017 David Caldwell <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

More Repositories

1

build-emacs

Build scripts for www.emacsformacosx.com
Rez
359
star
2

commit-patch

Commit patches to Darcs, Git, Mercurial, Bazaar, Monotone, Subversion, or CVS
Perl
21
star
3

gdisk

GUID disk partition editor
C
18
star
4

calmer-forest-theme

Darkish theme for Emacs with green & orange tint
Emacs Lisp
17
star
5

daemon-manager

Manage Daemons run and controlled by non root users
C++
15
star
6

pdp11dasm

PDP-11 Disassembler
C
13
star
7

libanalogtv

Analog TV simulation for Apple 2 emulators
C
10
star
8

JSML

Javascript object to DOM translator
HTML
10
star
9

patchutils

Patchutils is a small collection of programs that operate on patch files.
C
7
star
10

stm.js

ServeToMe clone written in Javascript/Node
JavaScript
7
star
11

http-log-to-statsd

Gather stats from custom apache/nginx logs and push them to statsd
Rust
6
star
12

Saver-View

A fork of Brian Nenninger's SaverLab, an excellent Mac OS X screensaver utility
Objective-C
4
star
13

jenkins-sparkle-appcast-plugin

Jenkins plugin to create Sparkle Appcasts.
Ruby
3
star
14

Time-Monotonic

Perl interface to monotonically increasing clocks
C++
3
star
15

cyrus-imsp-server

The IMSP server is used for storing application preferences and address book data in a shared location.
Perl
2
star
16

erscom

Elden Ring Seamless CoOp Manager
Rust
2
star
17

dterm

Multiuser serial port to telnet bridge
Ruby
2
star
18

pc

A Nifty Perl Calculator
Perl
2
star
19

electro

Static site generator
Perl
2
star
20

cert

Simple LetsEncrypt client
Ruby
2
star
21

File-Slurp-Unicode

Reading/Writing of Complete Files with Character Encoding Support
Perl
2
star
22

eol

Change line endings between mac, dos and unix
C
1
star
23

whatsmyip

Quicky HTTP server that just gives back the requester's IP address.
Rust
1
star
24

verbose-shell

sh -x like logging of Ruby's FileUtils functions
Ruby
1
star
25

libre-hardware-monitor-to-telegraf

Convert Libre Hardware Monitor's web API JSON into something more palatable to Telegraf
Go
1
star
26

pdpfs

Manipulate RT-11 Filesystems on disk images
Rust
1
star
27

apt-transport-s3-rust

Rust
1
star