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)
|
2 |
|
3 | Package for formatting JSON data in a coloured YAML-style, perfect for CLI output.
|
4 |
|
5 | ## How to install
|
6 |
|
7 | Just install it via NPM:
|
8 |
|
9 | ```bash
|
10 | $ npm install prettyjson
|
11 | ```
|
12 |
|
13 | ## Using it (from the CLI)
|
14 |
|
15 | This package installs a command line interface to render JSON data in a more convenient way. You can use the CLI
|
16 | in 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](http://rafeca.com/prettyjson/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
|
27 | the JSON result in a clearer way:
|
28 |
|
29 | ```bash
|
30 | $ curl https://api.github.com/users/rafeca | prettyjson
|
31 | ```
|
32 |
|
33 | ![Example 2](http://rafeca.com/prettyjson/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
|
36 | and they'll be automatically displayed in a clearer way:
|
37 |
|
38 | ![Example 3](http://rafeca.com/prettyjson/images/example5.png)
|
39 |
|
40 | If you install the package globally (with `npm install -g prettyjson`), the CLI will be installed automatically in your PATH
|
41 | thanks to npm.
|
42 |
|
43 | ### Customizing colors via command line
|
44 |
|
45 | Now 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 |
|
51 | The available options are `PRETTYJSON_KEYS`, `PRETTYJSON_DASH`, `PRETTYJSON_STRING` and `PRETTYJSON_INDENT`.
|
52 |
|
53 | ## Using it (from Node.js)
|
54 |
|
55 | It's pretty easy to use it... you just have to include it in your script and call the `render()` method:
|
56 |
|
57 | ```javascript
|
58 | var prettyjson = require('prettyjson');
|
59 |
|
60 | var data = {
|
61 | username: 'rafeca',
|
62 | url: 'https://github.com/rafeca',
|
63 | twitter_account: 'https://twitter.com/rafeca',
|
64 | projects: ['prettyprint', 'connfu']
|
65 | };
|
66 |
|
67 | console.log(prettyjson.render(data));
|
68 | ```
|
69 |
|
70 | And will output:
|
71 |
|
72 | ![Example 4](http://rafeca.com/prettyjson/images/example1.png)
|
73 |
|
74 | You 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
|
78 | var prettyjson = require('prettyjson');
|
79 |
|
80 | var data = {
|
81 | username: 'rafeca',
|
82 | url: 'https://github.com/rafeca',
|
83 | twitter_account: 'https://twitter.com/rafeca',
|
84 | projects: ['prettyprint', 'connfu']
|
85 | };
|
86 |
|
87 | console.log(prettyjson.render(data, {
|
88 | keysColor: 'rainbow',
|
89 | dashColor: 'magenta',
|
90 | stringColor: 'white'
|
91 | }));
|
92 | ```
|
93 |
|
94 | Will output something like:
|
95 |
|
96 | ![Example 5](http://rafeca.com/prettyjson/images/example2.png)
|
97 |
|
98 | ## Annotated source
|
99 |
|
100 | You can check the [annotated source](http://rafeca.com/prettyjson/prettyjson.html) for more information about how it works
|
101 |
|
102 | ## Running Tests
|
103 |
|
104 | To run the test suite first invoke the following command within the repo, installing the development dependencies:
|
105 |
|
106 | ```bash
|
107 | $ npm install --dev
|
108 | ```
|
109 |
|
110 | then run the tests:
|
111 |
|
112 | ```bash
|
113 | $ npm test
|
114 | ```
|
115 |
|
116 | You can check the package's [test coverage](http://rafeca.com/prettyjson/coverage.html) if you are one of those test paranoics
|