Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🦋 Pretty-print JSON data into HTML to indent and colorize (with TypeScript declarations)

License

NotificationsYou must be signed in to change notification settings

center-key/pretty-print-json

Repository files navigation

logo

Pretty-print JSON data into HTML to indent and colorize

License:MITnpmHitsBuild

Source is written in functional TypeScript, andpretty-print-json.min.js (minified) is 2.1 KB.

Screenshot

A) Try It Out

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

B) Setup

1. Web browser

Load from thejsdelivr.com CDN:

<linkrel=stylesheethref=https://cdn.jsdelivr.net/npm/pretty-print-json@3.0/dist/css/pretty-print-json.css>...<scriptsrc=https://cdn.jsdelivr.net/npm/pretty-print-json@3.0/dist/pretty-print-json.min.js></script>

The minified JS file is 2 KB.

Fordark mode, replacepretty-print-json.css withpretty-print-json.dark-mode.css inthe<link> tag.

Or to automatically sensedark mode based on theprefers-color-scheme CSS media feature, usepretty-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';

C) Usage

1. API

consthtml=prettyPrintJson.toHtml(data,options?);

2. Example

HTML:
<preid=accountclass=json-container></pre>
#"Permalink: JavaScript:" href="#javascript">

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

constdata={active:true,mode:'🚃',codes:[48348,28923,39080],city:'London',};constelem=document.getElementById('account');elem.innerHTML=prettyPrintJson.toHtml(data);

3. Options

Name (key)TypeDefaultDescription
indentinteger3Number of spaces for indentation.
lineNumbersbooleanfalseWrap HTML in an<ol> tag to support line numbers.*
linkUrlsbooleantrueCreate anchor tags for URLs.
linksNewTabbooleantrueAdd atarget=_blank attribute setting to anchor tags.
quoteKeysbooleanfalseAlways double quote key names.
trailingCommasbooleantrueAppend a comma after the last item in arrays and objects.

*When settinglineNumbers totrue, do not use the<pre> tag as thewhite-space: pre;styling is applied to each line (<li>).

Screenshot

D) TypeScript Declarations

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

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

Theoptions parameter is aFormatOptions object:

typeFormatOptions={indent?:number,//number of spaces for indentationlineNumbers?:boolean,//wrap HTML in an <ol> tag to support line numberslinkUrls?:boolean,//create anchor tags for URLslinksNewTab?:boolean,//add a target=_blank attribute setting to anchor tagsquoteKeys?:boolean,//always double quote key namestrailingCommas?: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';constdata={active:true,mode:'🚃',codes:[48348,28923,39080],city:'London',};constoptions:FormatOptions={linkUrls:true};consthtml:string=prettyPrintJson.toHtml(data,options);

E) Build Environment

Check out therunScriptsConfig section inpackage.json for aninteresting approach to organizing build tasks.

CLI Build Tools for package.json

  • 🎋add-dist-headerPrepend a one-line banner comment (with license notice) to distribution files
  • 📄copy-file-utilCopy or rename a file with optional package version number
  • 📂copy-folder-utilRecursively copy files from one folder to another folder
  • 🪺recursive-execRun a command on each file in a folder and its subfolders
  • 🔍replacer-utilFind and replace strings or template outputs in text files
  • 🔢rev-web-assetsRevision web asset filenames with cache busting content hash fingerprints
  • 🚆run-scripts-utilOrganize npm package.json scripts into groups of easy to manage commands
  • 🚦w3c-html-validatorCheck the markup validity of HTML files using the W3C validator


To see some example HTML results, runnpm install,npm test, and thennode spec/examples.js.

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

MIT License |Blog post


[8]ページ先頭

©2009-2025 Movatter.jp