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 |
|
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 -g prettyjson
|
11 | ```
|
12 |
|
13 | This will install `prettyjson` globally, so it will be added automatically
|
14 | to your `PATH`.
|
15 |
|
16 | ## Using it (from the CLI)
|
17 |
|
18 | This package installs a command line interface to render JSON data in a more
|
19 | convenient way. You can use the CLI in three different ways:
|
20 |
|
21 | **Decode a JSON file:** If you want to see the contents of a JSON file, just pass
|
22 | it as the first argument to the CLI:
|
23 |
|
24 | ```bash
|
25 | $ prettyjson package.json
|
26 | ```
|
27 |
|
28 | ![Example 1](https://raw.github.com/rafeca/prettyjson/master/images/example3.png)
|
29 |
|
30 | **Decode the stdin:** You can also pipe the result of a command (for example an
|
31 | HTTP request) to the CLI to see the JSON result in a clearer way:
|
32 |
|
33 | ```bash
|
34 | $ curl https://api.github.com/users/rafeca | prettyjson
|
35 | ```
|
36 |
|
37 | ![Example 2](https://raw.github.com/rafeca/prettyjson/master/images/example4.png)
|
38 |
|
39 | **Decode random strings:** if you call the CLI with no arguments, you'll get a
|
40 | prompt where you can past JSON strings and they'll be automatically displayed in a clearer way:
|
41 |
|
42 | ![Example 3](https://raw.github.com/rafeca/prettyjson/master/images/example5.png)
|
43 |
|
44 | ### Command line options
|
45 |
|
46 | It's possible to customize the output through some command line options:
|
47 |
|
48 | ```bash
|
49 | # Change colors
|
50 | $ prettyjson --string=red --keys=blue --dash=yellow --number=green package.json
|
51 |
|
52 | # Do not use colors
|
53 | $ prettyjson --nocolor=1 package.json
|
54 |
|
55 | # Change indentation
|
56 | $ prettyjson --indent=4 package.json
|
57 | ```
|
58 |
|
59 | **Deprecation Notice**: The old configuration through environment variables is
|
60 | deprecated and it will be removed in the next major version (1.0.0).
|
61 |
|
62 | ## Using it (from Node.js)
|
63 |
|
64 | It's pretty easy to use it. You just have to include it in your script and call
|
65 | the `render()` method:
|
66 |
|
67 | ```javascript
|
68 | var prettyjson = require('prettyjson');
|
69 |
|
70 | var data = {
|
71 | username: 'rafeca',
|
72 | url: 'https://github.com/rafeca',
|
73 | twitter_account: 'https://twitter.com/rafeca',
|
74 | projects: ['prettyprint', 'connfu']
|
75 | };
|
76 |
|
77 | var options = {
|
78 | noColor: true
|
79 | };
|
80 |
|
81 | console.log(prettyjson.render(data, options));
|
82 | ```
|
83 |
|
84 | And will output:
|
85 |
|
86 | ![Example 4](https://raw.github.com/rafeca/prettyjson/master/images/example1.png)
|
87 |
|
88 | You can also configure the colors of the hash keys and array dashes
|
89 | (using [colors.js](https://github.com/Marak/colors.js) colors syntax):
|
90 |
|
91 | ```javascript
|
92 | var prettyjson = require('prettyjson');
|
93 |
|
94 | var data = {
|
95 | username: 'rafeca',
|
96 | url: 'https://github.com/rafeca',
|
97 | twitter_account: 'https://twitter.com/rafeca',
|
98 | projects: ['prettyprint', 'connfu']
|
99 | };
|
100 |
|
101 | console.log(prettyjson.render(data, {
|
102 | keysColor: 'rainbow',
|
103 | dashColor: 'magenta',
|
104 | stringColor: 'white'
|
105 | }));
|
106 | ```
|
107 |
|
108 | Will output something like:
|
109 |
|
110 | ![Example 5](https://raw.github.com/rafeca/prettyjson/master/images/example2.png)
|
111 |
|
112 | ## Running Tests
|
113 |
|
114 | To run the test suite first invoke the following command within the repo,
|
115 | installing the development dependencies:
|
116 |
|
117 | ```bash
|
118 | $ npm install
|
119 | ```
|
120 |
|
121 | then run the tests:
|
122 |
|
123 | ```bash
|
124 | $ npm test
|
125 | ```
|
126 |
|
127 | On windows, you can run the tests with:
|
128 |
|
129 | ```cmd
|
130 | C:\git\prettyjson> npm run-script testwin
|
131 | ```
|