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

log-parsed-json

v0.0.53

Published

Pretty prints strings to console if it contains valid JSON

Downloads

105

Readme

log-parsed-json

GitHub Super-Linter Tests Publish to npmjs

Pretty prints JSON like objects found in the string passed to it.

Recursively dumps JSON objects found inside other JSON objects, even if they are overly stringified.

As well as regular stringified JSON, it copes with much of the output from util.inspect() for standard JSON-like data objects, including circular references.

Escaped quotes as used in Kibana logs are handled. E.g. {\"@metadata\": \"value\"}

Intended to help with debugging, particulary in situations where you have for example Kibana logs containing JSON within JSON.

Automatic Repairs to JSON

  • Change Python None to null
  • Change Python True and False to true and false
  • Insert missing commas between key-value pairs
  • Insert missing commas between array elements
  • Remove trailing commas
  • Add quotes to keys
  • Convert single quotes, backticks, curly quotes, escaped double quotes and double escaped double quotes to double quotes
  • Merge strings concatenated with a + to a single string
  • Remove additional double quote at start of key that gpt-3.5-turbo sometimes adds
  • Escape unescaped newline \n in string value
  • Deal with many escaping la-la land cases e.g. {\"res\": \"{ \\\"a\\\": \\\"b\\\" }\"}

Installation

npm install log-parsed-json

Find and handle the JSON output from Large Language Models

Sometimes large language models output JSON like strings, but with a few quirks. This can happen with gpt-3.5-turbo and gpt-4 for example.

const { firstJson } = require('log-parsed-json');

const completion = `Thought: "I need to search for developer jobs in London"
Action: SearchTool
ActionInput: { location: "London", 'title': "developer" }
`;

console.log(firstJson(completion));

Gives output

{ "location": "London", "title": "developer" }

Usage - pretty printing JSONs found within a string

const { log } = require('log-parsed-json');

log(`some text { key1: true, 'key2': "  { inner: 'value', } " } text { a: 1 } text`);

Result

Result

Usage - parsing a JSON like string into JSON.parse() friendly format

util.inspect()'s output is not JSON.parse() friendly.

const { repairJson } = require('log-parsed-json');

console.log(repairJson(`{ 'k1': 'v1', 'k2': 123 }`));

Result

{ "k1": "v1", "k2": 123 }

Mentions of circular are just turned into a string, and any refs within the object are removed.

console.log(repairJson("{ a: 'somestring', b: 42, e: { c: 82, d: [Circular *1] } }"));

Result

{ "a": "somestring", "b": 42, "e": { "c": 82, "d": ["Circular"] } }

Usage - parsing a string to an array of plain strings or JSON.parse() compatible strings

const { toArrayOfPlainStringsOrJson } = require('log-parsed-json');

console.log(toArrayOfPlainStringsOrJson(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`));

Result

[
  'text ',
  '{ "k1": "v1", "k2": 123 }',
  ' text ',
  '{ "a": 1 }',
  ' text'
]

Usage - canParseJson

Returns true or false based on if repairJson() would return a valid JSON string.

const { canParseJson } = require('log-parsed-json');

console.log(canParseJson(`{ 'k1': 'v1', k2: 123 }`));
console.log(canParseJson(`{ 'k1': "v1", "k2": 123 }`));
console.log(canParseJson(`"test"`));
console.log(canParseJson(123));
console.log(canParseJson(true));

Result

true
true
false
false
false

Let's write a function and compare the response from JSON.parse() for the same scenarios.

function isJSON(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

console.log(isJSON(`{ 'k1': 'v1', k2: 123 }`));
console.log(isJSON(`{ 'k1': "v1", "k2": 123 }`));
console.log(isJSON(`"test"`));
console.log(isJSON(123));
console.log(isJSON(true));

Result

false
false
true
true
true

Usage - firstJson

Returns the first JSON object found in the string.

const { firstJson } = require('log-parsed-json');

console.log(firstJson(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`));

Result

{ "k1": "v1", "k2": 123 }

Usage - lastJson

Returns the last JSON object found in the string.

const { lastJson } = require('log-parsed-json');

console.log(lastJson(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`));

Result

{ "a": 1 }

Usage - largestJson

Returns the largest JSON object found in the string.

const { largestJson } = require('log-parsed-json');

console.log(largestJson(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`));

Result

{ "k1": "v1", "k2": 123 }

Usage - jsonMatching

Returns the first JSON object found in the string that matches the given regular expression.

const { jsonMatching } = require('log-parsed-json');

console.log(jsonMatching(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`, /a: 1/));

Result

{ "a": 1 }

Usage - pretty print JSONs piped to pretty

To enable this, install log-parsed-json globally

npm install -g log-parsed-json

Now you can pipe to pretty

echo abc { a: 2 } abc | pretty

curl 'https://jsonplaceholder.typicode.com/todos/1' | pretty

See also

Python version of this project: https://pypi.org/project/fix-busted-json/