|
2 | 2 |
|
3 | 3 | [](https://www.npmjs.com/package/json-to-plain-text) |
4 | 4 | [](https://github.com/sumithemmadi/json-to-plain-text/stargazers) |
5 | | -[](https://github.com/sumithemmadi/json-to-plain-text) |
6 | | -[](https://github.com/sumithemmadi/json-to-plain-text/blob/main/LICENSE) |
| 5 | +[](https://www.npmjs.com/package/json-to-plain-text) |
| 6 | +[](LICENSE) |
7 | 7 | [](https://github.com/sumithemmadi/json-to-plain-text) |
8 | 8 | [](https://github.com/sumithemmadi/json-to-plain-text/issues) |
9 | 9 | [](https://github.com/sponsors/sumithemmadi) |
10 | | -[](https://packagephobia.now.sh/result?p=json-to-plain-text) |
11 | | -[](https://bundlephobia.com/package/json-to-plain-text@latest) |
| 10 | +[](https://packagephobia.com/result?p=json-to-plain-text) |
| 11 | +[](https://bundlephobia.com/package/json-to-plain-text@latest) |
12 | 12 | [](https://snyk.io/test/npm/json-to-plain-text) |
13 | 13 |
|
14 | | -`json-to-plain-text` is a npm module that converts JSON-like data or plain JavaScript objects to a formatted plain text representation. It allows you to convert JSON-like data or plain JavaScript objects into human-readable format. |
| 14 | +--- |
15 | 15 |
|
16 | | -## Installation |
| 16 | +## 📖 Overview |
17 | 17 |
|
18 | | -1. Install `json-to-plain-text` npm package. |
| 18 | +`json-to-plain-text` is a lightweight npm package that converts **JSON** into a clean, human-readable **plain-text**. |
19 | 19 |
|
20 | | - ```bash |
21 | | - npm install json-to-plain-text |
22 | | - ``` |
| 20 | +Think of it as `JSON.stringify()`, but prettier, friendlier, and tailored for **logs, debugging, and CLI output**. |
23 | 21 |
|
24 | | -2. Import the `jsonToPlainText` function: |
| 22 | +> [!NOTE] |
| 23 | +> From version **2.0.0+**, this package outputs plain text only. |
| 24 | +> If you need **colored output**, please use [version 1.1.4](https://www.npmjs.com/package/json-to-plain-text/v/1.1.4) . |
25 | 25 |
|
26 | | - ```js |
27 | | - import { jsonToPlainText } from "json-to-plain-text"; |
28 | | - ``` |
| 26 | +--- |
29 | 27 |
|
30 | | -## Usage |
| 28 | +## ✨ Features |
31 | 29 |
|
32 | | -The `jsonToPlainText` function accepts two parameters: `data` and `options`. |
| 30 | +- 🔹 Convert JSON objects into easy-to-read plain text |
| 31 | +- ⚡ Tiny & fast (zero dependencies) |
| 32 | +- 🛠 Configurable separators, spacing, quotes, and array formatting |
| 33 | +- ✅ Great for **logs, debugging, reports, and CLI tools** |
33 | 34 |
|
34 | | -### Parameters |
| 35 | +--- |
35 | 36 |
|
36 | | -- `data` (required): The input data to convert. It can be JSON-like data or plain JavaScript objects. |
| 37 | +## 🔍 Why use this? |
37 | 38 |
|
38 | | -- `options` (optional): Configuration options for customizing the output. It is an object with the following properties: |
| 39 | +Use `json-to-plain-text` when you need: |
| 40 | +- 📑 **Readable logs** – no more squinting at raw JSON |
| 41 | +- 🖥 **CLI outputs** – format objects neatly for the terminal |
| 42 | +- 🧪 **Debugging helpers** – quickly inspect nested objects |
| 43 | +- 📝 **Reports/configs** – human-friendly data printing |
39 | 44 |
|
40 | | - - `color` (boolean, default: `true`): Whether to apply colors to the output or not. Set to `true` to add colors using the `chalk` library or `false` for plain text output. |
41 | | - - `seperator` (string, default: `:`): seperate keys and values. |
| 45 | +--- |
42 | 46 |
|
43 | | - - `spacing` (boolean, default: `true`): Whether to include spacing before colons. Set to `true` for formatted spacing or `false` to remove spacing before colons. |
| 47 | +## 🚀 Installation |
44 | 48 |
|
45 | | - - `squareBracketsForArray` (boolean, default: `false`): Whether to use square brackets for arrays. Set to `true` to enclose arrays in square brackets or `false` to display arrays without brackets. |
| 49 | +```bash |
| 50 | +npm install json-to-plain-text |
| 51 | +``` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 📦 Usage |
| 56 | + |
| 57 | +```js |
| 58 | +import { jsonToPlainText } from "json-to-plain-text"; |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## ⚙️ API |
46 | 64 |
|
47 | | - - `doubleQuotesForKeys` (boolean, default: `false`): Whether to use double quotes for object keys. Set to `true` to wrap object keys in double quotes or `false` to keep them as they are. |
| 65 | +```ts |
| 66 | +jsonToPlainText(data: unknown, options?: Options): string |
| 67 | +``` |
| 68 | + |
| 69 | +### Parameters |
| 70 | + |
| 71 | +- **`data`** *(required)* – JSON-like input or JavaScript object. |
| 72 | +- **`options`** *(optional)* – Customize the formatting. |
| 73 | + |
| 74 | +| Option | Type | Default | Description | |
| 75 | +|---------------------------|---------|---------|-------------| |
| 76 | +| `separator` | string | `:` | Character used between keys and values. | |
| 77 | +| `spacing` | boolean | `true` | Align keys with spacing before colons. | |
| 78 | +| `squareBracketsForArray` | boolean | `false` | Display arrays inside `[]`. | |
| 79 | +| `doubleQuotesForKeys` | boolean | `false` | Wrap object keys in double quotes. | |
| 80 | +| `doubleQuotesForValues` | boolean | `false` | Wrap string values in double quotes. | |
48 | 81 |
|
49 | | - - `doubleQuotesForValues` (boolean, default: `false`): Whether to use double quotes for string values. Set to `true` to add double quotes around string values or `false` to display them without quotes. |
| 82 | +--- |
50 | 83 |
|
51 | | -### Example |
| 84 | +## 💡 Example |
52 | 85 |
|
53 | | -```typescript |
| 86 | +```ts |
54 | 87 | import { jsonToPlainText, Options } from "json-to-plain-text"; |
55 | 88 |
|
56 | | -let data = { |
| 89 | +const data = { |
57 | 90 | place_id: "173937105", |
58 | 91 | osm_type: "way", |
59 | | - osm_id: "319992693", |
60 | 92 | lat: "17.861533866867224", |
61 | 93 | lon: "78.8081441896764", |
62 | | - display_name: |
63 | | - "Satadar Nagar, Ward 116 Allapur, Hyderabad, Kukatpally mandal, Telangana, 500018, India", |
64 | 94 | address: { |
65 | | - neighbourhood: "Satadar Nagar", |
66 | | - suburb: "Ward 116 Allapur", |
67 | 95 | city: "Hyderabad", |
68 | | - county: "Kukatpally mandal", |
69 | 96 | state: "Telangana", |
70 | | - postcode: "500018", |
71 | 97 | country: "India", |
72 | | - country_code: "in", |
73 | 98 | }, |
74 | | - extratags: {}, |
75 | | - namedetails: {}, |
76 | | - boundingbox: ["17.8598497", "17.8623087", "78.8079136", "78.8082658"], |
77 | 99 | distance: 2, |
78 | 100 | }; |
79 | 101 |
|
80 | | - |
81 | | -// This is optional |
82 | 102 | const options: Options = { |
83 | | - color: true, // Whether to apply colors to the output or not |
84 | | - spacing: true, // Whether to include spacing before colons or not |
85 | | - seperator: ":", // seperate keys and values. |
86 | | - squareBracketsForArray: false, // Whether to use square brackets for arrays or not |
87 | | - doubleQuotesForKeys: false, // Whether to use double quotes for object keys or not |
88 | | - doubleQuotesForValues: false, // Whether to use double quotes for string values or not |
89 | | -} |
90 | | - |
91 | | -// Convert the data to formatted plain text representation |
92 | | -const plainText = jsonToPlainText(data, options); |
93 | | -console.log(plainText); |
| 103 | + separator: ":", |
| 104 | + spacing: true, |
| 105 | + squareBracketsForArray: false, |
| 106 | + doubleQuotesForKeys: false, |
| 107 | + doubleQuotesForValues: false, |
| 108 | +}; |
| 109 | + |
| 110 | +console.log(jsonToPlainText(data, options)); |
94 | 111 | ``` |
95 | 112 |
|
96 | | -### Output |
| 113 | +### ✅ Output |
97 | 114 |
|
98 | 115 | ```yaml |
99 | | -place_id : 173937105 |
100 | | -osm_type : way |
101 | | -osm_id : 319992693 |
102 | | -lat : 17.861533866867224 |
103 | | -lon : 78.8081441896764 |
104 | | -display_name : Satadar Nagar, Ward 116 Allapur, Hyderabad, Kukatpally mandal, Telangana, 500018, India |
105 | | -address : |
106 | | -neighbourhood : Satadar Nagar |
107 | | -suburb : Ward 116 Allapur |
108 | | -city : Hyderabad |
109 | | -county : Kukatpally mandal |
110 | | -state : Telangana |
111 | | -postcode : 500018 |
112 | | -country : India |
113 | | -country_code : in |
114 | | -extratags : {} |
115 | | -namedetails : {} |
116 | | -boundingbox : 17.8598497, 17.8623087, 78.8079136, 78.8082658 |
117 | | -distance : 2 |
| 116 | +place_id : 173937105 |
| 117 | +osm_type : way |
| 118 | +lat : 17.861533866867224 |
| 119 | +lon : 78.8081441896764 |
| 120 | +address : |
| 121 | +city : Hyderabad |
| 122 | +state : Telangana |
| 123 | +country : India |
| 124 | +distance : 2 |
118 | 125 | ``` |
119 | | -## 💝 Sponsor and support me |
120 | 126 |
|
121 | | -If you find my projects helpful or inspiring, consider supporting me through GitHub Sponsors. Your sponsorship helps me dedicate more time and effort to open source development and creating impactful projects. |
| 127 | +--- |
122 | 128 |
|
123 | | -[:heart: Sponsor me on github](https://github.com/sponsors/sumithemmadi?o=sd&sc=t) |
| 129 | +## 💝 Support |
124 | 130 |
|
125 | | -<a href='https://ko-fi.com/W7W4OZNLF' target='_blank'><img height='40' style='border:0px;height:40px;' src='https://storage.ko-fi.com/cdn/kofi3.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> |
| 131 | +If this project helps you, consider supporting my open-source work: |
126 | 132 |
|
127 | | -<a href="https://www.buymeacoffee.com/sumithemmadi"> <img align="left" src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" height="40" width="180" alt="sumithemmadi" /></a><br><br> |
| 133 | +- [💖 SPONSOR ME ON GITHUB](https://github.com/sponsors/sumithemmadi?o=sd&sc=t) |
128 | 134 |
|
129 | | -### 💖 Sponsors |
| 135 | +A huge thanks to all sponsors for supporting my work! |
130 | 136 |
|
131 | | -[](https://github.com/sponsors/sumithemmadi/) |
| 137 | +--- |
132 | 138 |
|
133 | | -- A huge thanks to my sponsors for their support. |
| 139 | +## 📜 License |
134 | 140 |
|
135 | | -## License |
| 141 | +This project is licensed under the [MIT License](LICENSE). |
136 | 142 |
|
137 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 143 | +--- |
138 | 144 |
|
139 | | -## Author |
| 145 | +## 👨💻 Author |
140 | 146 |
|
141 | | -- [Sumith Emmadi](https://github.com/sumithemmadi) |
| 147 | +- [Sumith Emmadi](https://github.com/sumithemmadi) |
0 commit comments