https://github.com/gera2ld/format-json
Format JSON in different flavors
https://github.com/gera2ld/format-json
formatter javascript json
Last synced: 6 months ago
JSON representation
Format JSON in different flavors
- Host: GitHub
- URL: https://github.com/gera2ld/format-json
- Owner: gera2ld
- License: mit
- Created: 2020-07-29T05:34:18.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-20T17:41:45.000Z (almost 3 years ago)
- Last Synced: 2025-03-19T19:40:11.148Z (over 1 year ago)
- Topics: formatter, javascript, json
- Language: TypeScript
- Homepage: https://gera2ld.github.io/format-json/
- Size: 201 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @gera2ld/format-json



Format JSON in different flavors.
## Features
- Format as JSON or JavaScript, either compact or indented
- Highlight nodes by changing the output on render
## Installation
```sh
$ yarn add @gera2ld/format-json
```
## Usage
```js
import { format, ItemTypes } from '@gera2ld/format-json';
const data = {/* … */};
const jsonOptions = {
indent: 0,
quoteAsNeeded: false,
quote: '"',
trailing: false,
template: false,
};
console.log('format as JSON:', format(data, jsonOptions));
const jsOptions = {
indent: 2,
quoteAsNeeded: true,
quote: '\'',
trailing: true,
template: true,
};
console.log('format as JavaScript', format(data, jsOptions));
const highlightOptions = {
onData(item) {
// if the property name is 'highlight', wrap it with `...`
if (item.path[item.path.length - 1] === 'highlight' && item.type === ItemTypes.KEY) {
item.data = [
{ value: '' },
...item.data,
{ value: '' },
];
}
},
};
console.log('highlight values', format(data, highlightOptions));
```