• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

๐Ÿฆ‹ Pretty-print JSON data into HTML to indent and colorize (with TypeScript declarations)

pretty-print-json

logo

Pretty-print JSON data into HTML to indent and colorize (written in functional TypeScript)

License:MIT npm Size Hits Build

Screenshot

A) Try It Out

Interactive online tool to format JSON:
https://pretty-print-json.js.org

B) Setup

1. Web browser

Load from the jsdelivr.com CDN:

<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/[email protected]/dist/css/pretty-print-json.css>
...
<script src=https://cdn.jsdelivr.net/npm/[email protected]/dist/pretty-print-json.min.js></script>

For dark mode, replace pretty-print-json.css with pretty-print-json.dark-mode.css in the <link> tag.

Or to automatically sense dark mode based on the prefers-color-scheme CSS media feature, use pretty-print-json.prefers.css instead.

2. Node.js server

Install package for node:

$ npm install pretty-print-json

Import into your application:

import { prettyPrintJson } from 'pretty-print-json';

Or for older CommonJS/UMD environments:

const { prettyPrintJson } = require('pretty-print-json');  //deprecated -- use ES modules instead

C) Usage

1. API

const html = prettyPrintJson.toHtml(data, options?);

2. Example

HTML:
<pre id=account class=json-container></pre>
JavaScript:

Pass data into prettyPrintJson.toHtml(data, options) and display the results.

const data = { active: true, mode: '๐Ÿšƒ', codes: [48348, 28923, 39080], city: 'London' };
const elem = document.getElementById('account');
elem.innerHTML = prettyPrintJson.toHtml(data);

3. Options

Name (key) Type Default Description
indent integer 3 Number of spaces for indentation.
lineNumbers boolean false Wrap HTML in an <ol> tag to support line numbers.*
linkUrls boolean true Create anchor tags for URLs.
linksNewTab boolean true Add a target=_blank attribute setting to anchor tags.
quoteKeys boolean false Always double quote key names.
trailingComma boolean true Append a comma after the last item in arrays and objects.

*When setting lineNumbers to true, do not use the <pre> tag as the white-space: pre; styling is applied to each line (<li>).

Screenshot

D) TypeScript Declarations

See the TypeScript declarations at the top of the pretty-print-json.ts file.

Customize the output of the function prettyPrintJson.toHtml(data: unknown, options?: FormatOptions) using the options parameter.

The options parameter is a FormatOptions object:

type FormatOptions = {
   indent?:        number,   //number of spaces for indentation
   lineNumbers?:   boolean,  //wrap HTML in an <ol> tag to support line numbers
   linkUrls?:      boolean,  //create anchor tags for URLs
   linksNewTab?:   boolean,  //add a target=_blank attribute setting to anchor tags
   quoteKeys?:     boolean,  //always double quote key names
   trailingComma?: boolean,  //append a comma after the last item in arrays and objects
   };

Example TypeScript usage with explicit types:

import { prettyPrintJson, FormatOptions } from 'pretty-print-json';

const data = { active: true, mode: '๐Ÿšƒ', codes: [48348, 28923, 39080], city: 'London' };
const options: FormatOptions = { linkUrls: true };
const html: string = prettyPrintJson.toHtml(data, options);

E) Build Environment

Check out the runScriptsConfig section in package.json for an interesting approach to organizing build tasks.

CLI Build Tools

  • ๐ŸŽ‹ add-dist-header:ย  Prepend a one-line banner comment (with license notice) to distribution files
  • ๐Ÿ“„ copy-file-util:ย  Copy or rename a file with optional package version number
  • ๐Ÿ“‚ copy-folder-util:ย  Recursively copy files from one folder to another folder
  • ๐Ÿ” replacer-util:ย  Find and replace strings or template outputs in text files
  • ๐Ÿ”ข rev-web-assets:ย  Revision web asset filenames with cache busting content hash fingerprints
  • ๐Ÿš† run-scripts-util:ย  Organize npm scripts into named groups of easy to manage commands
  • ๐Ÿšฆ w3c-html-validator:ย  Check the markup validity of HTML files using the W3C validator


To see some example HTML results, run npm install, npm test, and then node spec/examples.js.

Feel free to submit questions at:
github.com/center-key/pretty-print-json/issues

MIT License | Blog post

More Repositories

1

clabe-validator

๐Ÿ‡ฒ๐Ÿ‡ฝ Analyze or create a CLABE number for a Mexican bank account (with TypeScript declarations)
TypeScript
65
star
2

fetch-json

๐Ÿถ A wrapper around Fetch just for JSON (with TypeScript declarations)
TypeScript
30
star
3

paradise

๐Ÿ“ท Paradise PHP Photo Gallery
PHP
7
star
4

bookstore

Grails 3.0 sample application
Groovy
4
star
5

puppeteer-browser-ready

๐Ÿ•โ€๐Ÿฆบ Simple utility to go to a URL and wait for the HTTP response
TypeScript
4
star
6

replacer-util

๐Ÿ” Find and replace strings or template outputs in text files (CLI for package.json scripts)
TypeScript
4
star
7

reveal.js-local

๐Ÿ“ฝ๏ธ A single HTML file for showing a reveal.js presentation locally
HTML
4
star
8

rev-web-assets

๐Ÿ”ข Revision web asset filenames with cache busting content hash fingerprints (CLI for package.json scripts)
TypeScript
3
star
9

copy-file-util

๐Ÿ“„ Copy or rename a file (CLI for package.json scripts)
TypeScript
3
star
10

hljs-enhance

โœจ Adds a little extra stuff to highlight.js
HTML
3
star
11

add-dist-header

๐ŸŽ‹ Prepend a one-line banner comment with license notice to distribution files (CLI for package.json scripts)
TypeScript
3
star
12

run-scripts-util

๐Ÿš† Organize package.json scripts into named groups of easy to manage commands
TypeScript
3
star
13

hamburger-menu

โ˜ฐ A responsive CSS solution โ˜ฐ
JavaScript
3
star
14

assert-deep-strict-equal

โš–๏ธ Asynchronous assert fails in Mocha you can see and don't timeout (with TypeScript declarations)
TypeScript
2
star
15

gulp-node-slate

๐Ÿ““ node-slate as a gulp task
JavaScript
2
star
16

copy-folder-util

๐Ÿ“‚ Recursively copy a folder (CLI for package.json scripts)
TypeScript
2
star
17

server-listening

๐Ÿงช Simple promise to wait for server ready inside a mocha specification
TypeScript
2
star
18

cli-argv-util

๐Ÿ Simple utility to parse command line parameters and flags (arguments vector)
TypeScript
2
star
19

snappyfood

๐Ÿš Tastier than fast food, more convenient than sit-down
HTML
2
star
20

web-ignition

๐ŸŽ๏ธ Start a fire
HTML
1
star
21

grails-security-plugin

Straightforward security you can understand
Groovy
1
star
22

recursive-exec

๐Ÿชบ Run a command on each file in a folder and its subfolders (CLI for package.json scripts)
TypeScript
1
star
23

img-src-placeholder

๐Ÿ‘พ Replace src=# in <img> tags with a Base64 data URL of a 1 pixel image (CLI for package.json scripts)
TypeScript
1
star