# Install
You can use Jsonic in the browser and on the server.
You'll need to install the package into your project first:
$ npm install jsonic
To validate your install, run the following code:
console.log(Jsonic('a:1'))
This should output some strict JSON: {"a":1}
.
Jsonic also provides a command line utility that converts jsonic arguments into traditional JSON. If you install Jsonic globally, then you can run this utility directly:
$ npm install -g jsonic
$ jsonic a:1
{"a":1}
If you plan on writing custom plugins, this utility is very handy for
debugging (try jsonic -d
to get a debug log).
# Node.js
The recommended way to load Jsonic is:
const { Jsonic } = require('jsonic')
// And then just use it directly!
console.log(Jsonic('a:1'))
However, if you are upgrading from Jsonic 0.x or 1.x, then the old way will still work (and is not deprecated—this is a perfectly valid traditional Node.js idiom!):
const Jsonic = require('jsonic')
// Nothing broken!
console.log(Jsonic('a:1'))
# ES Module
If you are using Node version 14 or higher and want to load Jsonic into your own modules, then load the jsonic.js file directly, like so:
import Jsonic from 'jsonic'
console.log(Jsonic('a:1'))
This will also work:
import { Jsonic } from 'jsonic'
console.log(Jsonic('a:1'))
Note that Jsonic is a CommonJS module, so this required Node version 14 or higher.
In other scenarios (such as browser usage), your packaging solution (webpack, rollup, etc.) will expose Jsonic to you as an ES Module.
# Typescript
Jsonic is written in TypeScript, and the package includes type information. Import and go!
import Jsonic from 'jsonic'
console.log(Jsonic('a:1'))
This will also work:
import { Jsonic } from 'jsonic'
console.log(Jsonic('a:1'))
The package also exports all of the types needed for plugin development:
// Some of the exported types (there are more...)
import { Jsonic, Plugin, Rule, Context } from 'jsonic'
# Browser
If you use a packager (such as webpack, rollup, parcel, ...), then import Jsonic and the packager will look after things for you.
import { Jsonic } from 'jsonic'
You can also load Jsonic directly using the script
tag. The
package includes a standalone minified version jsonic.min.js
.
<script src="jsonic.min.js"></script>
This will make the global variable Jsonic
available for direct use.
# Getting Help
If you need help installing Jsonic, please post a question on the Installation (opens new window) discussion board.
# Next Steps
Follow the Getting Started guide to learn the extended Jsonic syntax.