Socket
Socket
Sign inDemoInstall

is-json

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

is-json - npm Package Compare versions

Comparing version 1.0.1 to 1.1.1

9

index.js

@@ -7,6 +7,13 @@

function isObject (obj) {
return obj === Object(obj) && !Array.isArray(obj);
}
module.exports = isJson;
function isJson (str) {
function isJson (str, pass_objects) {
if (pass_objects && isObject(str)) return true;
if (!isString(str)) return false;

@@ -13,0 +20,0 @@

2

package.json
{
"name": "is-json",
"version": "1.0.1",
"version": "1.1.1",
"description": "check if a string is a valid JSON string without using Try/Catch",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -8,17 +8,31 @@ # is-json

check if a string is a valid JSON string without using Try/Catch
check if a string is a valid JSON string without using Try/Catch and is a JSON object
**V1**
**V1.1**
var isJson = require('is-json');
var good_json = '{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}';
var bad_json = '{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}';
var str_number = '121212';
console.log(isJson(good_json)); // true
console.log(isJson(bad_json)); // false
console.log(isJson(str_number)); // false
isJson(str*, [passObjects=bool])
*with `passObjects = true` can pass a JSON object in `str`, default to `false`
var isJson = require('is-json');
var good_json = '{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}';
var bad_json = '{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}';
var str_number = '121212';
console.log(isJson(good_json)); // true
console.log(isJson(bad_json)); // false
console.log(isJson(str_number)); // false
// v1.1 now can check is a JSON object
var json = {a: 12, b: [1,2,3]};
console.log(isJson(json, true)); // true

@@ -20,5 +20,5 @@ var test = require('assert');

].forEach(function (ele, i) {
test.equal(isJson(ele), i < 9 ? false : true, 'Only the last element is a valid JSON string');
test.equal(i === 8 ? isJson(ele, true) : isJson(ele), i < 8 ? false : true, 'Only the two last elements are a valid JSON');
});
console.log('finish.');
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc