# format-to-json

> Format string to a json like template.

Latest version **4.0.0** (published 2025-08-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install format-to-json
pnpm add format-to-json
yarn add format-to-json
bun add format-to-json
```

Provides the command `fmt2json`.

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2025-08-26 |
| First published | 2020-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 150.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | CN-Tower |
| Maintainers | cntower |
| Keywords | fmt2json, format2json, json, zjson, toJson, beJson, str2json, string2json, str-to-json, string-to-json, fmt-to-json, format-to-json, json-kit, json-like, json-editor, json-tools, json-utils, json-formatter, json-algorithm |

## Links

- npm: https://www.npmjs.com/package/format-to-json
- Repository: https://github.com/CN-Tower/format-to-json
- Homepage: https://github.com/CN-Tower/format-to-json#readme
- Issues: https://github.com/CN-Tower/format-to-json/issues
- npm.io page: https://npm.io/package/format-to-json

## Dependencies (3)

- [funclib](https://npm.io/package/funclib.md) ^5.1.3
- [enquirer](https://npm.io/package/enquirer.md) ^2.3.6
- [commander](https://npm.io/package/commander.md) ^2.19.0

## 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

- 4.0.0 (latest) — 2025-08-26
- 3.0.6 — 2025-01-03
- 3.0.5 — 2024-07-04
- 3.0.4 — 2024-06-30
- 3.0.3 — 2023-01-31
- 3.0.2 — 2023-01-30
- 3.0.1 — 2023-01-30
- 3.0.0 — 2023-01-30
- 2.1.2 — 2021-04-06
- 2.1.1 — 2021-04-06
- 2.0.1 — 2021-04-05
- 1.0.5 — 2021-01-08
- 1.0.4 — 2020-10-10
- 1.0.3 — 2020-10-10
- 1.0.2 — 2020-10-04
- … 2 more at https://npm.io/package/format-to-json/versions

## README

# format-to-json

<p align="center">
  <a href="https://unpkg.com/format-to-json@3.0.3/index.html">
    <img src="https://github.com/CN-Tower/format-to-json/blob/master/images/format_html.png?raw=true">
  </a>
</p>

Playground: https://unpkg.com/format-to-json@3.0.3/index.html<br>

[![npm](https://img.shields.io/npm/v/format-to-json.svg)](https://www.npmjs.com/package/format-to-json)
[![LICENSE MIT](https://img.shields.io/npm/l/format-to-json.svg)](https://github.com/CN-Tower/format-to-json/blob/master/LICENSE)

> Format string to a json like template

- [Usages](#Usages)
  - [In html](#in-html)
  - [In javascript](#in-javascript)
- [Interface](#Interface)
  - [fmt2json](#mehtod-fmt2json)
  - [FormatOptions](#interface-formatoptions)
  - [FormatResult](#interface-formatresult)
- [Terminal](#Terminal)

## Usages

### In html

```html
<script src="https://unpkg.com/format-to-json@3.0.3/fmt2json.min.js"></script>
<script>
  const source = `{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http://zjson.net","project":"http://github.com/CN-Tower/zjson","language":["中文（简体）","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}`;

  const jsonString = fmt2json(source);
  console.log(jsonString);
  // =>
  `{
  "zjson": "ZJSON",
  "description:"Online json formatter",
  "version": "v4.1.8",
  "updateTime": "2018-11-23",
  "url": "http://zjson.net",
  "project": "http://github.com/CN-Tower/zjson",
  "language": [
    "中文（简体）",
    "English"
  ],
  "keywords": [
    "zjson",
    "json formatter"
  ],
  "content": {
    "array": [
      "element 001",
      "element 002"
    ],
    "boolean": true,
    "null": null,
    "number": 123,
    "string": "Hello World",
    "object": {
      "property": "value",
      "key": "val"
    }
  }
}`;
</script>
```

### In javascript

Run: `npm install format-to-json --save`;

```javascript
const fmt2json = require('format-to-json');
const source =
  '{"zjson":"ZJSON","description":"Online json formatter","version":"v4.1.8","updateTime":"2018-11-23","url":"http://zjson.net","project":"http://github.com/CN-Tower/zjson","language":["中文（简体）","English"],"keywords":["zjson","json formatter"],"content":{"array":["element 001","element 002"],"boolean":true,"null":null,"number":123,"string":"Hello World","object":{"property":"value","key":"val"}}}';

const fmtInfo = fmt2json(source, { withDetails: true });
console.log(fmtInfo.result);
```

Output:

```js
{
  result: '{\r\n' +
    '  "zjson": "ZJSON",\r\n' +
    '  "description": "Online json formatter",\r\n' +
    '  "version": "v4.1.8",\r\n' +
    '  "updateTime": "2018-11-23",\r\n' +
    '  "url": "http://zjson.net",\r\n' +
    '  "project": "http://github.com/CN-Tower/zjson",\r\n' +
    '  "language": [\r\n' +
    '    "中文（简体）",\r\n' +
    '    "English"\r\n' +
    '  ],\r\n' +
    '  "keywords": [\r\n' +
    '    "zjson",\r\n' +
    '    "json formatter"\r\n' +
    '  ],\r\n' +
    '  "content": {\r\n' +
    '    "array": [\r\n' +
    '      "element 001",\r\n' +
    '      "element 002"\r\n' +
    '    ],\r\n' +
    '    "boolean": true,\r\n' +
    '    "null": null,\r\n' +
    '    "number": 123,\r\n' +
    '    "string": "Hello World",\r\n' +
    '    "object": {\r\n' +
    '      "property": "value",\r\n' +
    '      "key": "val"\r\n' +
    '    }\r\n' +
    '  }',
  fmtType: 'danger',
  fmtSign: 'end',
  fmtLines: 29,
  message: 'Expect a comma or a "}" in line: 29',
  errFormat: true,
  errIndex: 29,
  errNear: '...": "val"\\n    }\\n  }>>>>>>',
  errExpect: '}'
}
```

## Interface

#### [Mehtod] fmt2json

```typescript
declare function fmt2json(source: string, options?: FormatOptions): string;
declare function fmt2json(source: string, options: FormatOptions & { withDetails: true }): FormatResult;
```

#### [Interface] FormatOptions

```typescript
interface FormatOptions {
  indent?: number; // Integer, Large then 0, default: 2
  expand?: boolean; // Default: true
  strict?: boolean; // Default: false
  escape?: boolean; // Default: false
  unscape?: boolean; // Default: false
  keyQtMark?: "'" | '"' | ''; // Default: "\""
  valQtMark?: "'" | '"'; // Default: "\""
}
```

#### [Interface] FormatResult

```typescript
interface FormatResult {
  result: string;
  fmtType: 'info' | 'success' | 'warning' | 'danger';
  fmtSign: 'ost' | 'col' | 'val' | 'end' | 'war' | 'scc' | 'err';
  fmtLines: number;
  message: string;
  errFormat: boolean;
  errIndex: number;
  errNear: string;
  errExpect: string;
}
```

## Terminal

Run: `npm install -g format-to-json`  
Run: `fmt2json -h`

```terminal
Usage: fmt2json [options]

Options:
  -V, --version          output the version number
  -v, --version          output the version number
  -i, --indent <indent>  Indnet number.
  -q, --qtMark <qtMark>  Quotation mark, one of ['""', "''", '"', "'"] (default: "\"\"")
  -c, --collapse         Collapse the formatted results.
  -e, --escape           Escape the formatted results.
  -u, --unescape         Unescape source before format.
  -s, --strict           Strict mode.
  -d, --details          Return with formatted details info.
  -h, --help             output usage information
```

Run: `fmt2json -i 4 -q "'" -d`

```terminal
√ Input a string to foramt: · [{name: "Tom", age: 28, gender: "male"}]

==================================================================
                [23:10:11] format-to-json(3.0.3)
------------------------------------------------------------------
[
    {
        name: 'Tom',
        age: 28,
        gender: 'male'
    }
]
------------------------------------------------------------------
{
  fmtType: 'success',
  fmtSign: 'scc',
  fmtLines: 8,
  message: 'Success formated 8 lines!',
  errFormat: false,
  errIndex: NaN,
  errNear: ''
  errExpect: '',
}
==================================================================
```

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