npm install @components-1812/json-visualizer<script type="module">
import JSONVisualizer from 'https://cdn.jsdelivr.net/npm/@components-1812/json-visualizer@0.0.3/src/JSONVisualizer.min.js';
//Load the stylesheet
JSONVisualizer.stylesSheets.links.push('https://cdn.jsdelivr.net/npm/@components-1812/json-visualizer@0.0.3/src/JSONVisualizer.min.css');
console.log(JSONVisualizer);
customElements.define('custom-json-visualizer', JSONVisualizer);
</script>-
jsdelivr:
JSON visualizer packageJSONVisualizer.jsJSONVisualizer.cssJSONTokenizer.js -
unpkg:
JSON visualizer packageJSONVisualizer.jsJSONVisualizer.cssJSONTokenizer.js
If you use Vite or a framework based on Vite such as Astro, you can import the component in a client-side script file:
import '@components-1812/json-visualizer';and use it in your HTML:
<custom-svg-isolate>
{
"name": "John Doe",
"age": null,
"isStudent": false,
"courses": ["Math", "Science"],
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"grades": [ 85, 92, 78 ],
"contact": {
"email": "john.doe@example.com",
"phone": "123-456-7890"
}
}
</custom-svg-isolate>
<custom-json-visualizer json='{"name": "John Doe", "age": null}'></custom-json-visualizer>
<custom-json-visualizer src="https://jsonplaceholder.typicode.com/todos/1"></custom-json-visualizer>Note:
If you are using a builder or framework that doesn't support import ?raw, you need to load the component and its stylesheets manually.
If you want to add custom stylesheets to the component or need to load stylesheets from a different path, you can do it like this:
-
Using your builder’s import raw method,
CSSStyleSheet, and the component’sAdoptedStyleSheetsproperty:import JSONVisualizer from "@components-1812/json-visualizer/JSONVisualizer.js"; import JSONVisualizerRawCSS from "@components-1812/json-visualizer/JSONVisualizer.css?raw"; //Add the stylesheets to the component const JSONVisualizerCSS = new CSSStyleSheet(); JSONVisualizerCSS.replaceSync(JSONVisualizerRawCSS); JSONVisualizer.stylesSheets.adopted.push(JSONVisualizerCSS); //Define the component import('@components-1812/json-visualizer/define');
-
Using a
<style>tag inside the shadow root of the component:import JSONVisualizer from "@components-1812/json-visualizer/JSONVisualizer.js"; import JSONVisualizerRawCSS from "@components-1812/json-visualizer/JSONVisualizer.css?raw"; //Add the stylesheets to the component JSONVisualizer.stylesSheets.raw.push(JSONVisualizerCSS); //Define the component import('@components-1812/json-visualizer/define');
-
Using a
<link>tag inside the shadow root of the component:import JSONVisualizer from "@components-1812/json-visualizer/JSONVisualizer.js"; import JSONVisualizerCSS from "@components-1812/json-visualizer/JSONVisualizer.css?url"; //Add the stylesheets to the component JSONVisualizer.stylesSheets.links.push(JSONVisualizerCSS); //Define the component import('@components-1812/json-visualizer/define');
Note:
import('@components-1812/json-visualizer/define')callscustomElements.define('custom-json-visualizer', JSONVisualizer);indefine.js
--line-height: 1.25;
--line-elements-gap: 5px;
--line-white-space: normal;
--font: Consolas, "Courier New", monospace, sans-serif;
--line-number-padding: 5px;
--line-number-color: #777;
--line-number-background: transparent;
--line-number-text-align: right;
--line-number-width: fit-content;
--line-number-min-width: 2ch;/* Set by JS according to JSON line count */
--toggle-button-width: 15px;
--toggle-button-color: #777;
--toggle-button-hover-color: #fff;
--toggle-button-padding: 0;
--copy-button-width: 40px;
--copy-button-height: 40px;
--copy-button-color: #777;
--copy-button-hover-color: #fff;
--copy-button-padding: 10px;
--indentation-guides-lines-color: #444;
--indentation-guides-lines-width: 1px;
--json-tab-size: 4ch;
/* Theme Colors */
--json-background: #222;
--json-foreground: #eee;
--json-key: #9cdcfe;
--json-string: #ce9178;
--json-number: #b5cea8;
--json-boolean: #569cd6;
--json-null: #dcdcaa;
--json-brace: #ffd700;
--json-bracket: #ffd700;
--json-comma: #d4d4d4;
--json-colon: #d4d4d4; You can use one of the existing themes located in the assets/themes folder.
Example with Vite (or other bundlers):
import "@components-1812/json-visualizer/assets/themes/nord.css"Example with CDN:
<link rel="stylesheet" href="https://unpkg.com/@components-1812/json-visualizer@0.0.3/assets/themes/dracula.css">Then apply the theme by using the corresponding class name:
<custom-json-visualizer class="dracula"></custom-json-visualizer>
<custom-json-visualizer class="nord"></custom-json-visualizer>Note:
All themes are based on the default
<custom-json-visualizer>tag name.If you register the custom element under a different name, you’ll need to adjust the theme’s CSS selectors accordingly.
Show all available themes in this example: Changing themes
If you want to add your own icons to the component, you can do it like this:
Use a <template> tag with the slot="icons" attribute, and assign a data-name to each icon using one of the following values: toggle, copy, or copy-done.
<custom-json-visualizer>
{
"name": "John Doe",
"age": null,
"isStudent": false,
"courses": ["Math", "Science"],
}
<template slot="icons">
<span data-name="toggle">🔽</span>
<span data-name="copy">📋</span>
<span data-name="copy-done">✅</span>
</template>
</custom-json-visualizer>Note:
You can retrieve icons using the
getIcon(name)method.To get a cloned copy of the icon instead of a reference, use
getIcon(name, { clone: true }).
The default tokenizer in JSONTokenizer.js has been tested a lot (see tests) and works well with any valid JSON input.
However, if you prefer to use your own tokenizer, you can override the default behavior via JSONVisualizer.getTokens:
import JSONVisualizer from "@components-1812/json-visualizer";
JSONVisualizer.getTokens = async (rawJson) => {
return [];//[{type, value, tags}, ...]
}Each token should follow this structure:
type Token = {
type:'brace-open' | 'brace-close' | 'bracket-open' | 'bracket-close' | 'colon' | 'comma' | 'string' | 'number' | 'boolean' | 'null',
value:string | boolean | number | null,
tags:Array<'brace' | 'bracket' | 'colon' | 'comma' | 'string' | 'number' | 'boolean' | 'null' | 'open' | 'close' | 'key' | 'value' | 'array-value'>,
}Note:
The
typeproperty is required for the JSON to render correctly.The
tagsarray is used for syntax highlighting.The
colorandurlproperties are used to render a visual preview or clickable link.
Aqui puedes ver un ejemplo usando babel-standalone: custom tokenizer
<!-- Default -->
<custom-json-visualizer colors="all"></custom-json-visualizer>
<!-- All color type supported -->
<custom-json-visualizer colors="hex, rgb, rgba, hsl, hsla, named"></custom-json-visualizer>
<!-- Disable color preview -->
<custom-json-visualizer colors="none"></custom-json-visualizer>const jsonVisualizer = document.querySelector('custom-json-visualizer');
jsonVisualizer.colors = 'all';
jsonVisualizer.colors = 'none';
jsonVisualizer.colors = null;
jsonVisualizer.colors = 'hex, rgb, rgba, hsl, hsla, named';
jsonVisualizer.colors = ['hex', 'rgb', 'rgba', 'hsl', 'hsla', 'named'];
jsonVisualizer.colors = new Set(['hex', 'rgb', 'rgba', 'hsl', 'hsla', 'named']);
jsonVisualizer.colors;//<- Set<String>Note:
<span>Tokens representing colors are marked withtags="color color-<color type> ..."
<!-- Default -->
<custom-json-visualizer urls="all"></custom-json-visualizer>
<!-- All URL types supported -->
<custom-json-visualizer urls="http, https, ftp, www, domain, relative, mail, phone"></custom-json-visualizer>
<!-- Disable clickable links -->
<custom-json-visualizer urls="none"></custom-json-visualizer>Note:
<span>Tokens representing URLs are marked withtags="url url-<url type> ..."
-
src: URL used to fetch a JSON file.If the
srcattribute is not set, the component will use either thejsonattribute or itstextContentduring initialization. -
json: A raw JSON string to render directly.
-
line-numbers: Controls whether line numbers are rendered. Set to"none"to hide them. -
toggle-lines: Controls whether toggle buttons for collapsible sections are shown. Set to"none"to hide them. -
indentation-guides-lines: Shows indentation guide lines. Set to"none"to hide them. -
copy-button: Controls whether the copy button are rendered. Set to"none"to hide it.
-
render-deepdefault:Infinity: Limits the initial rendering depth for deeply nested JSON.Any non-numeric value is treated as
Infinity.Deeper levels will only be rendered when expanded by the user.
-
colorsdefault:all: Controls that color types show a preview.Set to
colors="all"to enable color preview for all recognized color types:hex,rgb,rgba,hsl,hsla,namedSet to Comma-separated list:
colors="hex, rgb, rgba, hsl, hsla, named"Set to
colors="none"to hide it.
-
urlsdefault:all: Controls that URL types show a preview.Set to
urls="all"to enable clickable links for all recognized URL types:http,https,ftp,relative,mail,phoneSet to Comma-separated list:
urls="http, https, ftp, relative, mail, phone"Set to
urls="none"to hide it.
-
folded-message: The message displayed between lines when a section is folded. The default is: \22ef (middle dots: ⋯) when not set.Set a custom message to display when the section is folded, for example:
folded-message="folded items..."You can use the
{content_lenght}variable in the custom message:<custom-json-visualizer folded-message="{content_lenght} folded items..."></custom-json-visualizer>
Set
folded-message="none"to hide it.Note
Internally, the folded-message attribute is used to set the CSS variable
--json-folded-message-content.You can try setting it manually if needed, but the recommended way is to use the attribute directly
-
ready: Set when the component is fully initialized (at the end ofconnectedCallback()). -
ready-links: Set when external stylesheets fromJSONVisualizer.stylesSheets.linkshave finished loading. -
ready-json: Set when the JSON has been successfully loaded and rendered.
These properties reflect the corresponding HTML attributes:
-
json(reactive): A raw JSON string to render. Updating this will trigger a re-render. -
data(readonly): Returns the parsed result of the currentjsonvalue (JSON.parse(json)). If parsing fails, returnsnull. -
src(reactive): A URL to fetch a JSON file. Updating this will also trigger a re-render.
-
lineNumbers: Controls whether line numbers are shown. Use "none" to hide them. -
toggleLines: Controls whether toggle buttons are shown. -
indentationGuidesLines: Controls the visibility of indentation guide lines. -
copyButton: Controls the visibility of the copy button.
-
renderDeep: Limits the initial rendering depth for deeply nested JSON.Returns a
numberofrender-deepattribute orInfinity
-
colors: Controls which color types should display a visual previewReturns:
-
A
Set<string>containing the types listed in thecolors="rgb, hsl, ..."attribute. -
An empty
Setwhencolors="none". -
A
Setwith all supported color types (as defined byJSONVisualizer.defaults.colors) whencolors="all"
-
-
urls: Controls which URL types should be rendered as clickable linksReturns
-
A
Set<string>containing the types listed in theurls="http, https, ..."attribute. -
An empty
Setwhenurls="none" -
A
Setwith all supported URL types (as defined byJSONVisualizer.defaults.urls) whenurls="all"
-
-
async renderJSON(raw, options?)Clears the currently rendered JSON and renders the new one using the given options.
raw can be a string or parsed object.
-
clearJSON()Clears the currently rendered JSON content.
-
async loadJSON(url, options?)Fetches a JSON file from the specified URL and renders it using the given options.
-
getIcon(name, { clone = false })Retrieves one of the internal icons used for the toggle or copy buttons.
Valid
namevalues: "toggle", "copy", "copy-done".Pass
{ clone: true }to get a cloned copy of the icon instead of a reference. -
clearListeners()Removes all internal event listeners used by the component.
-
ready: Fired when the component has finished initializing (at the end ofconnectedCallback()). -
ready-links: Fired when the stylesheets defined inJSONVisualizer.stylesSheets.linksare loaded.document.querySelector('custom-json-visualizer').addEventListener('ready-links', (e) => { const {results} = e.detail; console.log(results);//[{link:HTMLLinkElement, href:string, status:'loaded' | 'error'}, ...] });
-
ready-json: Fired when the JSON has been successfully loaded and rendered. -
copy-done: Fired when the copy button is clicked and the JSON data has been copied to the clipboard.document.querySelector('custom-json-visualizer').addEventListener('copy-done', (e) => { const {data} = e.detail; });
import JSONTokenizer from '@components-1812/json-visualizer/JSONTokenizer.js';
const tokenizer = new JSONTokenizer();
tokenizer.tokenize(rawJson);
console.log(tokenizer.tokens);//[{type, value, tags}, ...]This package is distributed under the MIT license.
Default icons used in this package are sourced from the Bootstrap Icons project, licensed under the MIT license.
© 2019–2024 The Bootstrap Authors
