Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-alexander

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-alexander

Serenity Now! Forgiving JSON parser

  • 0.1.13
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.7K
increased by2.85%
Maintainers
1
Weekly downloads
 
Created
Source

Forgiving JSON Parser

Serenity now! A forgiving JSON parser 🙏

import { parseJSON } from 'json-alexander'

/* Normal Valid JSON */
parseJSON('{"valid": "works"}')
// -> {"valid": "works"}

/* Javascript objects */
parseJSON({ key: 'val' })
// -> { key: 'val' }

/* Malformed JSON */
parseJSON("{'malformed': 'works'}")
// -> {"malformed": "works"}

/* Unbalanced JSON */
parseJSON('{"unbalanced": "object"')
// -> {"unbalanced": "object" }

/* Javascript objects missing quotes */
parseJSON('{ hello: there }')
// -> { "hello": "there" }

Throws if value passed in is not parsable.

Other options

  • https://github.com/hua1995116/easy-json-parse
  • https://github.com/RyanMarcus/dirty-json
  • https://www.npmjs.com/package/try-json

Security concerns

This package was built for nicer arg parser for CLIs. e.g.

my-cli-command --data '{ foo: bar }'

If you need a JSON parser for your server, consider the safeParse export instead.

This package makes use of regular expressions when fixing malformed json. As a result, it may be vulnerable to a REDOS attack.

I've run the regex patterns through vuln-regex-detector & the patterns used appear to be safe. See tests/regex

Recommended usage for this package is in serverless functions where max timeouts on requests can be used. This mitigates risk of REDOS.

If using in long running server... use the default parseJSON at your own risk or better yet use safeParse if you want to ignore malformed JSON & disable the mechanism that leverage regex patterns.

Example:

import { safeParse } from 'json-alexander'

/* Normal Valid JSON */
safeParse('{"valid": "works"}')
// -> {"valid": "works"}

/* Javascript objects */
safeParse({ key: 'val' })
// -> { key: 'val' }

/* Malformed JSON */
safeParse("{'malformed': 'works'}")
// -> null no autofix

This code is not vulnerable to possible redos.

FAQs

Package last updated on 10 Sep 2024

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc