https://github.com/luyilin/json-format-highlight
Format json and highlight it as well (only 652 bytes).
https://github.com/luyilin/json-format-highlight
formatter highlight json
Last synced: 5 months ago
JSON representation
Format json and highlight it as well (only 652 bytes).
- Host: GitHub
- URL: https://github.com/luyilin/json-format-highlight
- Owner: luyilin
- License: mit
- Created: 2018-02-11T10:56:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-07T20:31:54.000Z (over 3 years ago)
- Last Synced: 2025-10-04T02:29:24.868Z (9 months ago)
- Topics: formatter, highlight, json
- Language: JavaScript
- Homepage: https://luyilin.github.io/json-format-highlight/example/dist/
- Size: 1.52 MB
- Stars: 38
- Watchers: 1
- Forks: 4
- Open Issues: 36
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-format-highlight
[](https://npmjs.com/package/json-format-highlight) [](https://npmjs.com/package/json-format-highlight) [](https://circleci.com/gh/luyilin/json-format-highlight/tree/master)
[DEMO](https://luyilin.github.io/json-format-highlight/example/dist/)
[](https://codesandbox.io/s/github/luyilin/json-format-highlight/tree/master/example/codesandbox)
Snapshot

## Introduction
Format json and highlight it as well.
* Really tiny
* No dependency on other library
* Handles HTML tags within field values
* Handles quotes and other special characters within field values
* Styling is self-contained, no need to setup CSS
Note:
* May not be suitable for handling huge (such as 10MB) JSON.
## Install
```bash
yarn add json-format-highlight --save
```
CDN: [UNPKG](https://unpkg.com/json-format-highlight@latest/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/json-format-highlight@latest/dist/)
## Usage
### In Node.js
```js
import formatHighlight from 'json-format-highlight'
formatHighlight(json, colorOptions)
```
### In HTML
```html
You can pass the URL in query parameter url of this page,
then the JSON data would be fetched from that URL and displayed below:
function getParameterByName(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
$(function(){
$.ajax(
{
'url': getParameterByName('url'),
type: 'GET',
dataType: 'json',
error: function(jqXHR, textStatus, error) {
var errMsg = '' + textStatus + ' (' + error + ')'
document.write('<pre>\n' + errMsg + '\n</pre>')
},
success: function(data, textStatus, jqXHR) {
var formatted = jsonFormatHighlight(data)
document.write('<pre>\n' + formatted + '\n</pre>')
}
}
);
})
```
## Option
### colorOptions
Type: `object`
Fields:
|Option|Type|Default|Desc|
|---|---|---|---|
|keyColor|string|```dimgray```|Key color|
|numberColor|string|```lightskyblue```|Color of the value of the number type|
|stringColor|string|```lightcoral```|Color of the value of the string type|
|trueColor|string|```lightseagreen```|Color of the value of the boolean type and equals to true|
|falseColor|string|```#f66578```|Color of the value of the string type and equals to false|
|nullColor|string|```cornflowerblue```|Color of the value of the null type|
If you don't pass in anything this argument, default colors would be used. If you do pass in an object for this argument,
the colors you specified would override the default color scheme.
Feel free to use your own exclusive color scheme! :D
Below is an example color scheme that many people may like:
```js
customColorOptions = {
keyColor: 'black',
numberColor: 'blue',
stringColor: '#0B7500',
trueColor: '#00cc00',
falseColor: '#ff8080',
nullColor: 'cornflowerblue'
};
```
## License
MIT © [luyilin](https://github.com/luyilin)
> [minemine.cc](https://minemine.cc) · GitHub [@luyilin](https://github.com/luyilin) · Twitter [@luyilin12](https://twitter.com/luyilin12)