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

@acusti/parsing

v0.14.1

Published

Loosely parse a string as JSON with numerous affordances for syntax errors

Downloads

913

Readme

@acusti/parsing

latest version maintenance status bundle size downloads per month

@acusti/parsing exports parseAsJSON, a function that takes a string and attempts to parse it as JSON, returning the resulting JS value, or null if the string defeated all attempts at parsing it. This is especially useful for generative AI when you prompt an LLM to generate a response in JSON, because most models are unable to consistently generate valid JSON, and even when they do, will often have a pre- or post-amble as a part of the response.

The unit tests show the kinds of LLM responses and syntax errors that the package can fix and convert into a valid result.

Usage

npm install @acusti/parsing
# or
yarn add @acusti/parsing

Import parseAsJSON (it’s a named export) and pass a string to it:

import { parseAsJSON } from '@acusti/parsing';

// it might neglect to close the outer curly braces
parseAsJSON(`  Sure, here's an example of a JSON response for the "Contact Form" page:
{
    "heading": "Get in Touch",
    "form": {
        "email": "[email protected]",
        "message": "Please enter your message or inquiry below"
    }
`);
/* results in:
{
    heading: 'Get in Touch',
    form: {
        email: '[email protected]',
        message: 'Please enter your message or inquiry below',
    },
}
*/

// you might get a "key": "value" list with no syntax around it
parseAsJSON(` Here are the props for the "Blog" page:
Props:
"blogPostImage1": "/images/blog-post-image1.jpg",
"blogPostSubheading1": "Exploring the Art of Sourdough Baking",
"blogPostHeading1": "The Magic of Sourdough",
"blogPostLede1": "At Masa Madre, we're passionate about creating the perfect sourdough bread. Learn more about the art and craft of this ancient tradition.",
"blogPostImage2": "/images/blog-post-image2.jpg",
"blogPostSubheading2": "From Seed to Loaf",
"blogPostHeading2": "Our Journey to Your Table",
"blogPostLede2": "Discover the journey of our sourdough bread, from the seed to the loaf.",
"blogPostImage3": "/images/blog-post-image3.jpg",
"blogPostSubheading3": "Sourdough 101",
"blogPostHeading3": "Learn the Basics of Artisanal Bread Making",
"blogPostLede3": "Get started on your sourdough journey with our beginner's guide to artisanal bread making.",
`);
/* results in:
{
    blogPostImage1: '/images/blog-post-image1.jpg',
    blogPostSubheading1: 'Exploring the Art of Sourdough Baking',
    blogPostHeading1: 'The Magic of Sourdough',
    blogPostLede1: "At Masa Madre, we're passionate about creating the perfect sourdough bread. Learn more about the art and craft of this ancient tradition.",
    blogPostImage2: '/images/blog-post-image2.jpg',
    blogPostSubheading2: 'From Seed to Loaf',
    blogPostHeading2: 'Our Journey to Your Table',
    blogPostLede2: 'Discover the journey of our sourdough bread, from the seed to the loaf.',
    blogPostImage3: '/images/blog-post-image3.jpg',
    blogPostSubheading3: 'Sourdough 101',
    blogPostHeading3: 'Learn the Basics of Artisanal Bread Making',
    blogPostLede3: "Get started on your sourdough journey with our beginner's guide to artisanal bread making.",
}
*/

// in case llama 2 decides to give you a markdown table to represent the JSON
parseAsJSON(`Here are the props for Vytas' Hatha Yoga classes:
Props:
| Prop Name | Value |
| blogPostImage1 | /vytas-yoga-class-background.jpg |
| shortBlogPostCaption1 | "Find inner peace and balance through physical postures and breathing techniques" |
| blogPostHeading1 | "Hatha Yoga Classes with Vytas" |
| miniBlogPostLede1 | "Discover the transformative power of Hatha Yoga with Vytas, a seasoned yoga teacher" |
| blogPostImage2 | /vytas-yoga-community-background.jpg |
| shortBlogPostCaption2 | "Join a supportive community of like-minded individuals and deepen your practice with Vytas" |
| blogPostHeading2 | "Community and Connection" |
| miniBlogPostLede2 | "Vytas' Hatha Yoga classes offer a sense of community and connection" | |`);
/* results in:
{
    'Prop Name': 'Value',
    blogPostImage1: '/vytas-yoga-class-background.jpg',
    shortBlogPostCaption1: 'Find inner peace and balance through physical postures and breathing techniques',
    blogPostHeading1: 'Hatha Yoga Classes with Vytas',
    miniBlogPostLede1: 'Discover the transformative power of Hatha Yoga with Vytas, a seasoned yoga teacher',
    blogPostImage2: '/vytas-yoga-community-background.jpg',
    shortBlogPostCaption2: 'Join a supportive community of like-minded individuals and deepen your practice with Vytas',
    blogPostHeading2: 'Community and Connection',
    miniBlogPostLede2: "Vytas' Hatha Yoga classes offer a sense of community and connection",
}
*/

// it might prematurely close the outer JSON object even though the content continues
parseAsJSON(
    '```json\n{"heading":"Organic Produce","subheading":"The Benefits of Going Organic","description":"Organic produce is grown without the use of synthetic pesticides, herbicides, or fertilizers."},"items":[{"heading":"Organic Fruits","subheading":"Nature\'s Sweet Treats"},{"heading":"Organic Vegetables","subheading":"Fresh from the Garden"}]}\n```',
);
/* results in:
{
    heading: 'Organic Produce',
    subheading: 'The Benefits of Going Organic',
    description: 'Organic produce is grown without the use of synthetic pesticides, herbicides, or fertilizers.',
    items: [
        {
            heading: 'Organic Fruits',
            subheading: "Nature's Sweet Treats",
        },
        {
            heading: 'Organic Vegetables',
            subheading: 'Fresh from the Garden',
        },
    ],
}
*/

Again, there are more examples of the kinds of things that the parser can handle in the unit tests.

Also, if you’re wondering the best way to get an LLM to return JSON, I found the few-shot prompting approach suggested in Pinecone’s Llama 2: AI Developers Handbook helpful with a variety of different models (Llama 2 7B, OpenChat 3.5, OpenHermes 2.5, Zephyr 7B).