π A JavaScript library for converting between HTML and JSON, with binding, templating, attributes, and CSS support.
v2.3.0
- π Zero-dependencies and extremely lightweight!
- π¦Ύ Written in TypeScript, allowing greater support for React, Angular, and other libraries!
- π» Full API available via public functions.
- π Full support for Attributes, CSS style properties, and formatted text!
- β Write your JSON directly to any DOM element for rendering, or get the base element for use elsewhere.
- π Copy the layout for an entire page without additional files!
- π Apply additional filters to exclude specific node types, CSS styles, attributes, etc.
- π Data templating for text (with defaults support).
- π₯ Write CSS directly back to the head for each element!
- π§ Bind JSON directly to DOM elements!
- π· Include and write images as Base 64 URLs.
All modern browsers (such as Google Chrome, FireFox, and Opera) are fully supported.
To see a list of all the most recent changes, click here.
You can install the library with npm into your local modules directory using the following command:
npm install jhson.js
Or, you can download the latest zipped up version here.
Or, you can also use the following CDN links:
https://cdn.jsdelivr.net/gh/williamtroup/[email protected]/dist/jhson.min.js
https://cdn.jsdelivr.net/gh/williamtroup/[email protected]/dist/jhson.export.js
To get started using JHson.js, do the following steps:
Make sure you include the "DOCTYPE html" tag at the top of your HTML, as follows:
<!DOCTYPE html>
<script src="dist/jhson.js"></script>
<div id="header" class="header">
<h1>JHson.js</h1>
<p>This is a {{template_type | basic}} example of how to use <b>JHson.js</b> ... with template data {{template_data}}.</p>
</div>
<script>
var header = document.getElementById( "header" );
var json = $jhson
.json()
.includeAttributes( true )
.includeDataAttributes( true )
.includeCssProperties( true )
.includeText( true )
.includeChildren( true )
.includeImagesAsBase64( false )
.friendlyFormat( true )
.indentSpaces( 2 )
.ignoreNodeTypes( "q" )
.ignoreNodeCondition( null )
.ignoreCssProperties( "padding" )
.ignoreAttributes( "data-your-attribute" )
.generateUniqueMissingIds( false )
.generateUniqueMissingNames( false )
.propertyReplacer( null )
.get( header );
$jhson
.html()
.json( json )
.templateData( { "{{template_data}}": "this template data" } )
.removeOriginalAttributes( true )
.removeOriginalDataAttributes( true )
.clearOriginalHTML( true )
.addCssToHead( false )
.clearCssFromHead( false )
.logTemplateDataWarnings( false )
.addAttributes( true )
.addDataAttributes( true )
.addCssProperties( true )
.addText( true )
.addChildren( true )
.insertBefore( false )
.write( header );
var element = $jhson
.html()
.json( json )
.templateData( { "{{template_data}}": "this template data" } )
.removeOriginalAttributes( true )
.removeOriginalDataAttributes( true )
.clearOriginalHTML( true )
.addCssToHead( false )
.clearCssFromHead( false )
.logTemplateDataWarnings( false )
.addAttributes( true )
.addDataAttributes( true )
.addCssProperties( true )
.addText( true )
.addChildren( true )
.get();
</script>
<div data-jhson-js="{ 'json': 'json string' }">
Your HTML.
</div>
To see a list of all the available binding options you can use for "data-jhson-js", click here.
To see a list of all the available custom triggers you can use for "data-jhson-js", click here.
That's it! Nice and simple. Please refer to the code if you need more help (fully documented).
To customize, and get more out of JHson.js, please read through the following documentation.
To see a list of all the public functions available, click here.
Configuration options allow you to customize how JHson.js will function. You can set them as follows:
<script>
$jhson.setConfiguration( {
safeMode: false
} );
</script>
To see a list of all the available configuration options you can use, click here.