# log-parsed-json

> Pretty prints strings to console if it contains valid JSON

Latest version **0.0.72** (published 2026-01-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install log-parsed-json
pnpm add log-parsed-json
yarn add log-parsed-json
bun add log-parsed-json
```

Provides the command `pretty`.

## Health

**Score 50/100 (C)** — status: stable.

Positive: no vulnerabilities; has provenance.

Warnings: low downloads; no types; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.0.72 |
| Published | 2026-01-18 |
| First published | 2022-12-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 107.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Qarj |
| Maintainers | qarj |
| Keywords | colour, log, json, pretty, print, console, cli, terminal, debug, color, parse |

## Links

- npm: https://www.npmjs.com/package/log-parsed-json
- Repository: https://github.com/Qarj/log-parsed-json
- Homepage: https://github.com/Qarj/log-parsed-json#readme
- Issues: https://github.com/Qarj/log-parsed-json/issues
- npm.io page: https://npm.io/package/log-parsed-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.72 (latest) — 2026-01-18
- 0.0.71 — 2026-01-18
- 0.0.70 — 2026-01-18
- 0.0.69 — 2026-01-18
- 0.0.68 — 2025-12-10
- 0.0.67 — 2025-12-10
- 0.0.61 — 2025-12-10
- 0.0.59 — 2024-06-18
- 0.0.58 — 2024-06-18
- 0.0.57 — 2024-06-18
- 0.0.56 — 2024-05-14
- 0.0.55 — 2024-05-12
- 0.0.54 — 2024-05-12
- 0.0.53 — 2024-03-08
- 0.0.52 — 2024-01-23
- … 51 more at https://npm.io/package/log-parsed-json/versions

## README

# log-parsed-json

![Tests](https://github.com/Qarj/log-parsed-json/workflows/Tests/badge.svg)

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\\\" }\"}`
-   Ignore array index labels sometimes printed by loggers: `0:`, `0 =>`, `0 =`, `[0] =>`
Example of repairing logged array index labels:

```txt
{
  arr: [
    0: "a"
    1 => "b"
    [2] => "c"
  ]
}
```

Result

```txt
{ "arr": ["a", "b", "c"] }
```


## Aggressive Repairs

-   Mode to attempt to repair mismatched quotes as output by quantised models like Llama3
-   Mode to attempt repair of missing value quotes

## Installation

```bash
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.

```javascript
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

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

## Enable aggressive handling of quotes

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

const completion = `A JSON parser! { name: "Alice', ”age': 26 }
`;

console.log(firstJson(completion, { attemptRepairOfMismatchedQuotes: true }));
```

Gives output

```json
{ "name": "Alice", "age": 26 }
```

## Enable aggressive handling of missing value quotes

```javascript
const { firstJson } = require('../index.js');

const completion = `A JSON parser! { "name": Alice, age: 26, isAlive: true }`;

console.log(firstJson(completion, { attemptRepairOfMissingValueQuotes: true }));
```

Gives output

```json
{ "name": "Alice", "age": 26, "isAlive": true }
```

## Usage - pretty printing JSONs found within a string

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

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

Result

![Result](./jsonInJson.png)

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

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

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

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

Result

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

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

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

Result

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

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

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

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

Result

```txt
[
  '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.

```javascript
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

```json
true
true
false
false
false
```

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

```javascript
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

```json
false
false
true
true
true
```

## Usage - firstJson

Returns the first JSON object found in the string.

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

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

Result

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

## Usage - lastJson

Returns the last JSON object found in the string.

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

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

Result

```json
{ "a": 1 }
```

## Usage - largestJson

Returns the largest JSON object found in the string.

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

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

Result

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

## Usage - jsonMatching

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

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

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

Result

```json
{ "a": 1 }
```

## Usage - pretty print JSONs piped to `pretty`

To enable this, install `log-parsed-json` globally

```bash
npm install -g log-parsed-json
```

Now you can pipe to `pretty`

```sh
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/](https://pypi.org/project/fix-busted-json/)

---
_Source: https://npm.io/package/log-parsed-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
