UNPKG

3.38 kBMarkdownView Raw
1# prettyjson [![Build Status](https://secure.travis-ci.org/rafeca/prettyjson.png)](http://travis-ci.org/rafeca/prettyjson) [![NPM version](https://badge.fury.io/js/prettyjson.png)](http://badge.fury.io/js/prettyjson) [![Coverage Status](https://coveralls.io/repos/rafeca/prettyjson/badge.png?branch=master)](https://coveralls.io/r/rafeca/prettyjson?branch=master)
2
3Package for formatting JSON data in a coloured YAML-style, perfect for CLI output.
4
5## How to install
6
7Just install it via NPM:
8
9```bash
10$ npm install prettyjson
11```
12
13## Using it (from the CLI)
14
15This package installs a command line interface to render JSON data in a more convenient way. You can use the CLI
16in three different ways:
17
18**Decode a JSON file:** If you want to see the contents of a JSON file, just pass it as the first argument to the CLI:
19
20```bash
21$ prettyjson package.json
22```
23
24![Example 1](https://raw.github.com/rafeca/prettyjson/master/images/example3.png)
25
26**Decode the stdin:** You can also pipe the result of a command (for example an HTTP request) to the CLI to see
27the JSON result in a clearer way:
28
29```bash
30$ curl https://api.github.com/users/rafeca | prettyjson
31```
32
33![Example 2](https://raw.github.com/rafeca/prettyjson/master/images/example4.png)
34
35**Decode random strings:** if you call the CLI with no arguments, you'll get a prompt where you can past JSON strings
36and they'll be automatically displayed in a clearer way:
37
38![Example 3](https://raw.github.com/rafeca/prettyjson/master/images/example5.png)
39
40If you install the package globally (with `npm install -g prettyjson`), the CLI will be installed automatically in your PATH
41thanks to npm.
42
43### Customizing colors via command line
44
45Now it's possible to customize the colors of the output via environment variables, thanks to @bahamas10:
46
47```bash
48$ PRETTYJSON_KEYS=red PRETTYJSON_DASH=blue PRETTYJSON_STRING=yellow prettyjson package.json
49```
50
51The available options are `PRETTYJSON_KEYS`, `PRETTYJSON_DASH`, `PRETTYJSON_STRING` and `PRETTYJSON_INDENT`.
52
53## Using it (from Node.js)
54
55It's pretty easy to use it... you just have to include it in your script and call the `render()` method:
56
57```javascript
58var prettyjson = require('prettyjson');
59
60var data = {
61 username: 'rafeca',
62 url: 'https://github.com/rafeca',
63 twitter_account: 'https://twitter.com/rafeca',
64 projects: ['prettyprint', 'connfu']
65};
66
67console.log(prettyjson.render(data));
68```
69
70And will output:
71
72![Example 4](https://raw.github.com/rafeca/prettyjson/master/images/example1.png)
73
74You can also configure the colors of the hash keys and array dashes
75(using [colors.js](https://github.com/Marak/colors.js) colors syntax):
76
77```javascript
78var prettyjson = require('prettyjson');
79
80var data = {
81 username: 'rafeca',
82 url: 'https://github.com/rafeca',
83 twitter_account: 'https://twitter.com/rafeca',
84 projects: ['prettyprint', 'connfu']
85};
86
87console.log(prettyjson.render(data, {
88 keysColor: 'rainbow',
89 dashColor: 'magenta',
90 stringColor: 'white'
91}));
92```
93
94Will output something like:
95
96![Example 5](https://raw.github.com/rafeca/prettyjson/master/images/example2.png)
97
98## Running Tests
99
100To run the test suite first invoke the following command within the repo, installing the development dependencies:
101
102```bash
103$ npm install
104```
105
106then run the tests:
107
108```bash
109$ npm test
110```
111
112On windows, you can run the tests with:
113
114```cmd
115C:\git\prettyjson> npm run-script testwin
116```