json-from-script
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i json-from-script

Weekly Downloads

27

Version

2.0.0

License

MIT

Unpacked Size

11.1 kB

Total Files

12

Last publish

Collaborators

  • robinvdvleuten