You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

json-promise

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-promise

Promise based JSON parser. Handles invalid JSON data gracefully.

1.1.8
latest
Source
npmnpm
Version published
Weekly downloads
139K
-2.51%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

json

FAQs

Package last updated on 02 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts