npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-autocorrect

v0.2.1

Published

JSON parser that fixes your commas and quotes. Standalone function with bonus React component included

Downloads

8

Readme

JSON Autocorrect

Try it out: json.chrmcg.io

Why?

  • JSON is annoying to write & edit because we're used to writing JS objects, in which:
    • You don't have to put quotes around object keys
    • You can use single-quotes if that's your preference
    • Trailing commas are allowed (and useful when moving lines around)
  • So here's a JSON parser that corrects your quotes and commas for you!
    • You can ignore commas completely! (As long as you use newlines)
    • Never quote another key! Wanna write {foo: "bar", 42: "baz"}? Go for it!
      • You do still have to quote strings
        • But you can use single-quotes!

Usage

  • As a React component (see it in action):
    • npm install json-autocorrect
    • or yarn add json-autocorrect
import JsonEditor from 'json-autocorrect';

// ...

render() {
    return (
        <JsonEditor
            json={this.state.myJsonObject}
            onChange={obj => this.setState({myJsonObject: obj})}
        />
    );
}
  • As a standalone parser (drop-in replacement for JSON.parse):
import { parse } from 'json-autocorrect';

const myAlmostCorrectJson = `
{
    "foo": "bar",
    "baz": 5,
}
`;

// Throws an error because of the trailing comma
JSON.parse(myAlmostCorrectJson);

// Returns the object you wanted
parse(myAlmostCorrectJson);

How the React component works

It's a big text box. As you type, the border color indicates the parseability of the JSON within:

  • Red border: JSON cannot be parsed
  • Blue border: JSON is valid (or close enough to be corrected)
  • No border: JSON hasn't changed (is identical to props.json value)

When you unfocus the textarea, if the border is blue, the component will:

  • forgivingly parse your JSON, adding/removing commas and quotes as needed
  • concisely format the result
  • pass the corrected and formatted JSON to the onChange prop

Contributing

  • Append #debug to the demo URL, or pass debug={true} to the component, to enable some useful console logs
  • Add some tests!
  • Send a PR!
  • Try it out and open an issue!

License