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-like-parse

v1.0.3

Published

JavaScript npm module that finds JSON-like text within a string and then parses it on best effort basis

Downloads

1,058

Readme

json-like-parse

JavaScript npm module that finds JSON-like text within a string and then parses it on best effort basis.

Why

The primary use case for this module is to parse output generated by Artificial Intelligence (AI) systems and Large Language Models (LLMs), such as OpenAI's GPT series. When interacting with these models, developers often use format specifying prompts to request structured data like JSON. However, due to the nature of AI-generated text, the output might not always be valid JSON. This can lead to parsing issues when using traditional JSON parsers.

This module offers a best-effort approach to finding and parsing JSON-like text within the AI-generated output, helping developers handle potentially malformed JSON.

For example, consider the following AI-generated responses:

  1. AI: {"name": "John", "age": 28, "city": "New York"} and he loves playing soccer.
  2. {"product": "laptop", "price": 1200, "currency": "USD"

In both cases, the responses contain JSON-like text but are not strictly valid JSON. The first response has extra text bewfore and after the JSON object, and the second response is missing a closing brace because the response got truncated (due to max_tokens for example). Using a traditional JSON parser would throw an error, but this module attempts to parse the JSON-like text as accurately as possible, making it easier to work with AI-generated content.

Usage

To use this module in another JavaScript file, you can require it like this:

const findAndParseJsonLikeText = require('json-like-parse');

const text = 'Some text before {"key1": "value1", "key2": {"key3": "value3"}} some text after';

const parsedJsonObjects = findAndParseJsonLikeText(text);

console.log(parsedJsonObjects); // [ { key1: 'value1', key2: { key3: 'value3' } } ]

Installation

To install the package, run:

npm install json-like-parse

Node.js Package

Tests

To run tests, execute the following command in the module directory:

npm test

Limitations

This module uses a regex-based approach to matching JSON-like text, which has some limitations and may not work for all valid JSON strings, especially those with more complex nested structures. The best-effort-json-parser module will do its best to parse the JSON-like text, but it may not always produce accurate results for malformed JSON.

It is generally better to use a proper JSON parser when working with JSON data. This module should be used with caution and only for specific use cases where a best-effort approach to parsing JSON-like text is acceptable.

License

MIT License