json-from-script

Tiny JSON parser for your CSP aware script tags


Keywords
json, script, csp, security, javascript
License
MIT
Install
npm install json-from-script@2.0.0

Documentation

json-from-script

Tiny JSON parser for your Content Security Policy aware script tags.

NPM version Build Status Gzip size Install size

Installation

$ npm install --save json-from-script

Usage

Real Example on JSFiddle ➡️

When your HTML contains any script tag like this;

<script type="application/json" class="data" data-attr="foo">{&quot;bar&quot;:&quot;baz&quot;}</script>

You can parse it in your Javascript application like this;

import jsonFromScript from 'json-from-script';

// Parsed will be { foo: { bar: 'baz' } }
const parsed = jsonFromScript();

API

jsonFromScript(selector, attribute)

Creates a new instance of the parser with the given options. The following options can alternatively be provided to configure the parser for your specific needs:

  • selector <String>: The selector for the script tags to parse. (default: script.data)
  • attribute <String>: The data attribute to use when setting the attribute on the parsed object. (default: data-attr)

License

MIT © Robin van der Vleuten