JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 142815
  • Score
    100M100P100Q149562F
  • License MIT

Promise based JSON parser. Handles invalid JSON data gracefully.

Package Exports

  • json-promise

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Coverage Status

Introduction

Parse and stringify JSON data using promise to gracefully handle success and failures if the data is invalid. See the examples below for usage instructions. This module use bluebird for Promise/A+ support.

Installation

npm install json-promise

Usage Instructions

Parsing JSON data

var json = require('json-promise');
var str = [
    '{"menu":{"id":"file","value":"File","popup":' 
    ,'{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},' 
    ,'{"value":"Open","onclick":"OpenDoc()"},{"value":"Close",' 
    ,'"onclick":"CloseDoc()"}]}}}'
].join('');

json.parse(str)
    .then(function onParse(obj) {
        // do something with the data object
    })
    .catch(function onParseError(e) {
        // the data is corrupted!
    });

Stringify JSON data

var json = require('json-promise');
var obj = {
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
};

json.stringify(obj)
    .then(function onStringify(obj) {
        // do something with the string
    })
    .catch(function onStringifyError(e) {
        // the data is corrupted!
    });

Testing

npm test