{"id":16125632,"url":"https://github.com/phrogz/neatjson","last_synced_at":"2025-04-04T19:13:05.698Z","repository":{"id":30477907,"uuid":"34031970","full_name":"Phrogz/NeatJSON","owner":"Phrogz","description":"Pretty-print your JSON in Ruby, JS, or Lua with more power than JSON.stringify or JSON.pretty_generate","archived":false,"fork":false,"pushed_at":"2023-03-17T21:00:04.000Z","size":213,"stargazers_count":102,"open_issues_count":7,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-24T14:29:27.605Z","etag":null,"topics":["javascript","json","lua","ruby"],"latest_commit_sha":null,"homepage":"http://phrogz.net/JS/NeatJSON","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Phrogz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-04-16T03:20:02.000Z","updated_at":"2024-06-18T14:04:06.489Z","dependencies_parsed_at":"2024-06-18T14:04:06.155Z","dependency_job_id":"77682609-4728-4a79-ac1a-dcc2fe3cb726","html_url":"https://github.com/Phrogz/NeatJSON","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phrogz%2FNeatJSON","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phrogz%2FNeatJSON/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phrogz%2FNeatJSON/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Phrogz%2FNeatJSON/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Phrogz","download_url":"https://codeload.github.com/Phrogz/NeatJSON/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234923,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","json","lua","ruby"],"created_at":"2024-10-09T21:30:17.922Z","updated_at":"2025-04-04T19:13:05.678Z","avatar_url":"https://github.com/Phrogz.png","language":"Lua","readme":"# NeatJSON\n\n[![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)\n\nPretty-print your JSON in Ruby or JavaScript or Lua with more power than is provided by `JSON.pretty_generate` (Ruby) or `JSON.stringify` (JS). For example, like Ruby's `pp` (pretty print), NeatJSON can keep objects on one line if they fit, but break them over multiple lines if needed.\n\n**Features:**\n\n* [Online webpage](http://phrogz.net/JS/NeatJSON) for performing conversions and experimenting with options.\n  * _Modifying graphical options on the webpage also gives you the JS code you would need to call to get the same results._\n* Keep multiple values on one line, with variable wrap width.\n* Format numeric values to specified decimal precision.\n  * Optionally force specific keys to use floating point representation instead of bare integers for whole number values (e.g. `42.0` instead of `42`).\n* Sort object keys to be in alphabetical order.\n* Arbitrary whitespace (or really, any string) for indentation.\n* \"Short\" wrapping uses fewer lines, indentation based on values. (See last example below.)\n* Indent final closing bracket/brace for each array/object.\n* Adjust number of spaces inside array/object braces.\n* Adjust number of spaces before/after commas and colons (both for single- vs. multi-line).\n* Line up the values for an object across lines.\n* [Lua only] Produce Lua table serialization.\n\n\n## Table of Contents\n\n* [Installation](#installation)\n* [Usage](#usage)\n* [Examples](#examples)\n* [Options](#options)\n* [License \u0026 Contact](#license--contact)\n* [TODO (aka Known Limitations)](#todo-aka-known-limitations)\n* [History](#history)\n\n\n## Installation\n\n* Ruby: `gem install neatjson`\n* JavaScript (web): Clone the GitHub repo and copy `javascript/neatjson.js`\n* Node.js: `npm install neatjson`\n\n\n## Usage\n\n**Ruby**:\n\n~~~ ruby\nrequire 'neatjson'\njson = JSON.neat_generate( value, options )\n~~~\n\n\n**JavaScript (web)**:\n\n~~~ html\n\u003cscript type=\"text/javascript\" src=\"neatjson.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n    var json = neatJSON( value, options );\n\u003c/script\u003e\n~~~\n\n\n**Node.js**:\n\n~~~ js\nconst { neatJSON } = require('neatjson');\nvar json = neatJSON( value, options );\n~~~\n\n\n**Lua**:\n\n~~~ lua\nlocal neatJSON = require'neatjson'\nlocal json = neatJSON(value, options)\n~~~\n\n## Examples\n\n_The following are all in Ruby, but similar options apply in JavaScript and Lua._\n\n~~~ ruby\nrequire 'neatjson'\n\no = { b:42.005, a:[42,17], longer:true, str:\"yes\\nplease\" }\n\nputs JSON.neat_generate(o)\n#=\u003e {\"b\":42.005,\"a\":[42,17],\"longer\":true,\"str\":\"yes\\nplease\"}\n\nputs JSON.neat_generate(o, sort:true)\n#=\u003e {\"a\":[42,17],\"b\":42.005,\"longer\":true,\"str\":\"yes\\nplease\"}\n\nputs JSON.neat_generate(o,sort:true,padding:1,after_comma:1)\n#=\u003e { \"a\":[ 42, 17 ], \"b\":42.005, \"longer\":true, \"str\":\"yes\\nplease\" }\n\nputs JSON.neat_generate(o, sort:true, wrap:40)\n#=\u003e {\n#=\u003e   \"a\":[42,17],\n#=\u003e   \"b\":42.005,\n#=\u003e   \"longer\":true,\n#=\u003e   \"str\":\"yes\\nplease\"\n#=\u003e }\n\nputs JSON.neat_generate(o, sort:true, wrap:40, decimals:2)\n#=\u003e {\n#=\u003e   \"a\":[42,17],\n#=\u003e   \"b\":42.01,\n#=\u003e   \"longer\":true,\n#=\u003e   \"str\":\"yes\\nplease\"\n#=\u003e }\n\nputs JSON.neat_generate(o, sort:-\u003e(k){ k.length }, wrap:40, aligned:true)\n#=\u003e {\n#=\u003e   \"a\"     :[42,17],\n#=\u003e   \"b\"     :42.005,\n#=\u003e   \"str\"   :\"yes\\nplease\",\n#=\u003e   \"longer\":true\n#=\u003e }\n\nputs JSON.neat_generate(o, sort:true, wrap:40, aligned:true, around_colon:1)\n#=\u003e {\n#=\u003e   \"a\"      : [42,17],\n#=\u003e   \"b\"      : 42.005,\n#=\u003e   \"longer\" : true,\n#=\u003e   \"str\"    : \"yes\\nplease\"\n#=\u003e }\n\nputs JSON.neat_generate(o, sort:true, wrap:40, aligned:true, around_colon:1, short:true)\n#=\u003e {\"a\"      : [42,17],\n#=\u003e  \"b\"      : 42.005,\n#=\u003e  \"longer\" : true,\n#=\u003e  \"str\"    : \"yes\\nplease\"}\n\na = [1,2,[3,4,[5]]]\nputs JSON.neat_generate(a)\n#=\u003e [1,2,[3,4,[5]]]\n\nputs JSON.pretty_generate(a) # oof!\n#=\u003e [\n#=\u003e   1,\n#=\u003e   2,\n#=\u003e   [\n#=\u003e     3,\n#=\u003e     4,\n#=\u003e     [\n#=\u003e       5\n#=\u003e     ]\n#=\u003e   ]\n#=\u003e ]\n\nputs JSON.neat_generate( a, wrap:true, short:true )\n#=\u003e [1,\n#=\u003e  2,\n#=\u003e  [3,\n#=\u003e   4,\n#=\u003e   [5]]]\n\ndata = [\"foo\",\"bar\",{dogs:42,piggies:{color:'pink', tasty:true},\n        barn:{jimmy:[1,2,3,4,5],jammy:3.141592653,hot:\"pajammy\"},cats:7}]\n\nopts = { short:true, wrap:60, decimals:3, sort:true, aligned:true,\n         padding:1, after_comma:1, around_colon_n:1 }\n\nputs JSON.neat_generate( data, opts )\n#=\u003e [ \"foo\",\n#=\u003e   \"bar\",\n#=\u003e   { \"barn\"    : { \"hot\"   : \"pajammy\",\n#=\u003e                   \"jammy\" : 3.142,\n#=\u003e                   \"jimmy\" : [ 1, 2, 3, 4, 5 ] },\n#=\u003e     \"cats\"    : 7,\n#=\u003e     \"dogs\"    : 42,\n#=\u003e     \"piggies\" : { \"color\":\"pink\", \"tasty\":true } } ]\n~~~\n\n\n## Options\nYou may pass any of the following options to `neat_generate` (Ruby) or `neatJSON` (JavaScript/Lua).\n\n**Note**: camelCase option names below use snake_case in Ruby. For example:\n\n~~~ js\n// JavaScript\nvar json = neatJSON( myValue, { arrayPadding:1, afterComma:1, beforeColonN:2 } );\n~~~\n\n~~~ lua\n-- Lua\nlocal json = neatJSON( myValue, { arrayPadding=1, afterComma=1, beforeColonN=2 } )\n~~~\n\n~~~ ruby\n# Ruby\njson = JSON.neat_generate my_value, array_padding:1, after_comma:1, before_colon_n:2\n~~~\n\n* `wrap`              — Maximum line width before wrapping. Use `false` to never wrap, `true` to always wrap. default:`80`\n* `indent`            — Whitespace used to indent each level when wrapping. default:`\"  \"` (two spaces)\n* `indentLast`        — Indent the closing bracket/brace for arrays and objects? default:`false`\n* `short`             — Put opening brackets on the same line as the first value, closing brackets on the same line as the last? default:`false`\n  * _This causes the `indent` and `indentLast` options to be ignored, instead basing indentation on array and object padding._\n* `sort`              — Sort objects' keys in alphabetical order (`true`), or supply a lambda for custom sorting. default:`false`\n  * If you supply a lambda to the `sort` option, it will be passed three values: the (string) name of the key, the associated value, and the object being sorted, e.g. `{ sort:-\u003e(key,value,hash){ Float(value) rescue Float::MAX } }`\n* `aligned`           — When wrapping objects, line up the colons (per object)? default:`false`\n* `decimals`          — Decimal precision for non-integer numbers; use `false` to keep values precise. default:`false`\n* `trimTrailingZeros` — Remove extra zeros at the end of floats, e.g. `1.2000` becomes `1.2`. default:`false`\n* `forceFloats`       — Force every integer value written to the file to be a float, e.g. `12` becomes `12.0`. default:`false`\n* `forceFloatsIn`     — Specify an array of object key names under which all integer values are treated as floats.\n  For example, serializing `{a:[1, 2, {a:3, b:4}], c:{a:5, d:6}` with `forceFloatsIn:['a']` would produce `{\"a\":[1.0, 2.0, {\"a\":3.0, \"b\":4}], \"c\":{\"a\":5.0, \"d\":6}}`.\n* `arrayPadding`      — Number of spaces to put inside brackets for arrays. default:`0`\n* `objectPadding`     — Number of spaces to put inside braces for objects.  default:`0`\n* `padding`           — Shorthand to set both `arrayPadding` and `objectPadding`. default:`0`\n* `beforeComma`       — Number of spaces to put before commas (for both arrays and objects). default:`0`\n* `afterComma`        — Number of spaces to put after commas (for both arrays and objects). default:`0`\n* `aroundComma`       — Shorthand to set both `beforeComma` and `afterComma`. default:`0`\n* `beforeColon1`      — Number of spaces before a colon when the object is on one line. default:`0`\n* `afterColon1`       — Number of spaces after a colon when the object is on one line. default:`0`\n* `beforeColonN`      — Number of spaces before a colon when the object is on multiple lines. default:`0`\n* `afterColonN`       — Number of spaces after a colon when the object is on multiple lines. default:`0`\n* `beforeColon`       — Shorthand to set both `beforeColon1` and `beforeColonN`. default:`0`\n* `afterColon`        — Shorthand to set both `afterColon1` and `afterColonN`. default:`0`\n* `aroundColon`       — Shorthand to set both `beforeColon` and `afterColon`. default:`0`\n* `lua`               — (Lua only) Output a Lua table literal instead of JSON? default:`false`\n* `emptyTablesAreObjects` — (Lua only) Should `{}` in Lua become a JSON object (`{}`) or JSON array (`[]`)? default:`false` (array)\n\nYou may omit the 'value' and/or 'object' parameters in your `sort` lambda if desired. For example:\n\n~~~ ruby\n# Ruby sorting examples\nobj = {e:3, a:2, c:3, b:2, d:1, f:3}\n\nJSON.neat_generate obj, sort:true                              # sort by key name\n#=\u003e {\"a\":2,\"b\":2,\"c\":3,\"d\":1,\"e\":3,\"f\":3}\n\nJSON.neat_generate obj, sort:-\u003e(k){ k }                        # sort by key name (long way)\n#=\u003e {\"a\":2,\"b\":2,\"c\":3,\"d\":1,\"e\":3,\"f\":3}\n\nJSON.neat_generate obj, sort:-\u003e(k,v){ [-v,k] }                 # sort by descending value, then by ascending key\n#=\u003e {\"c\":3,\"e\":3,\"f\":3,\"a\":2,\"b\":2,\"d\":1}\n\nJSON.neat_generate obj, sort:-\u003e(k,v,h){ h.values.count(v) }    # sort by count of keys with same value\n#=\u003e {\"d\":1,\"a\":2,\"b\":2,\"e\":3,\"c\":3,\"f\":3}\n~~~\n\n~~~ js\n// JavaScript sorting examples\nvar obj = {e:3, a:2, c:3, b:2, d:1, f:3};\n\nneatJSON( obj, {sort:true} );                                              // sort by key name\n// {\"a\":2,\"b\":2,\"c\":3,\"d\":1,\"e\":3,\"f\":3}\n\nneatJSON( obj, { sort:function(k){ return k }} );                          // sort by key name (long way)\n// {\"a\":2,\"b\":2,\"c\":3,\"d\":1,\"e\":3,\"f\":3}\n\nneatJSON( obj, { sort:function(k,v){ return -v }} );                       // sort by descending value\n// {\"e\":3,\"c\":3,\"f\":3,\"a\":2,\"b\":2,\"d\":1}\n\nvar countByValue = {};\nfor (var k in obj) countByValue[obj[k]] = (countByValue[obj[k]]||0) + 1;\nneatJSON( obj, { sort:function(k,v){ return countByValue[v] } } );         // sort by count of same value\n// {\"d\":1,\"a\":2,\"b\":2,\"e\":3,\"c\":3,\"f\":3}\n~~~\n\n_Note that the JavaScript and Lua versions of NeatJSON do not provide a mechanism for cascading sort in the same manner as Ruby._\n\n\n## License \u0026 Contact\n\nNeatJSON is copyright ©2015–2023 by Gavin Kistner and is released under\nthe [MIT License](http://www.opensource.org/licenses/mit-license.php).\nSee the LICENSE.txt file for more details.\n\nFor bugs or feature requests please open [issues on GitHub][1].\nFor other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).\n\n\n## TODO (aka Known Limitations)\n\n* [Ruby] Figure out the best way to play with custom objects that use `to_json` for their representation.\n* Detect circular references.\n* Possibly allow \"JSON5\" output (legal identifiers unquoted, etc.)\n\n\n## History\n\n* **v0.10.6** — March 17, 2023\n  * Add TypeScript definitions for JavaScript library\n\n* **v0.10.5** — November 17, 2022\n  * Fix issue #21: Strings containing `#` get an invalid escape added (Ruby only)\n\n* **v0.10.4** — November 17, 2022\n  * Online tool shows input/output bytes\n\n* **v0.10.2** — August 31, 2022\n  * Fix bugs found in JavaScript version related to `trim_trailing_zeros`.\n\n* **v0.10.1** — August 29, 2022\n  * Fix bugs found when `force_floats_in` was combined with wrapping.\n  * Update interactive HTML tool to support new features.\n\n* **v0.10** — August 29, 2022\n  * Add `force_floats` and `force_floats_in` to support serialization for non-standard parsers that differentiate between integers and floats.\n  * Add `trim_trailing_zeros` option to convert the `decimals` output from e.g. `5.40000` to `5.4`.\n  * Convert JavaScript version to require ECMAScript 6 for performance.\n\n* **v0.9** — July 29, 2019\n  * Add Lua version, serializing to both JSON and Lua table literals\n  * All languages serialize Infinity/-Infinity to JSON as `9e9999` and `-9e9999`\n  * All languages serialize NaN to JSON as `\"NaN\"`\n\n* **v0.8.4** — May 3, 2018\n  * Fix issue #27: Default sorting fails with on objects with mixed keys [Ruby only]\n    * _Thanks Reid Beels_\n\n* **v0.8.3** — February 20, 2017\n  * Fix issue #25: Sorting keys on multi-line object **using function** does not work without \"short\" [JS only]\n    * _Thanks Bernhard Weichel_\n\n* **v0.8.2** — December 16th, 2016\n  * Fix issue #22: Sorting keys on multi-line object does not work without \"short\" [JS only]\n  * Update online interface to support tabs as well as spaces.\n  * Update online interface to use a textarea for the output (easier to select and copy).\n  * Update online interface turn off spell checking for input and output.\n\n* **v0.8.1** — April 22nd, 2016\n  * Make NeatJSON work with [Opal](http://opalrb.org) (by removing all in-place string mutations)\n\n* **v0.8** — April 21st, 2016\n  * Allow `sort` to take a lambda for customized sorting of object key/values.\n\n* **v0.7.2** — April 14th, 2016\n  * Fix JavaScript library to support objects without an `Object` constructor (e.g. `location`).\n  * Online HTML converter accepts arbitrary JavaScript values as input in addition to JSON.\n\n* **v0.7.1** — April 6th, 2016\n  * Fix Ruby library to work around bug in Opal.\n\n* **v0.7** — March 26th, 2016\n  * Add `indentLast`/`indent_last` feature.\n\n* **v0.6.2** — February 8th, 2016\n  * Use memoization to avoid performance stalls when wrapping deeply-nested objects/arrays.\n    _Thanks @chroche_\n\n* **v0.6.1** — October 12th, 2015\n  * Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)\n    * _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._\n\n* **v0.6** — April 26th, 2015\n  * Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.\n\n* **v0.5** — April 19th, 2015\n  * Do not format integers (or floats that equal their integer) using `decimals` option.\n  * Make `neatJSON()` JavaScript available to Node.js as well as web browsers.\n  * Add (Node-based) testing for the JavaScript version.\n\n* **v0.4** — April 18th, 2015\n  * Add JavaScript version with online runner.\n\n* **v0.3.2** — April 16th, 2015\n  * Force YARD to use Markdown for documentation.\n\n* **v0.3.1** — April 16th, 2015\n  * Remove some debugging code accidentally left in.\n\n* **v0.3** — April 16th, 2015\n  * Fix another bug with `short:true` and wrapping array values inside objects.\n\n* **v0.2** — April 16th, 2015\n  * Fix bug with `short:true` and wrapping values inside objects.\n\n* **v0.1** — April 15th, 2015\n  * Initial release.\n\n[1]: https://github.com/Phrogz/NeatJSON/issues\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphrogz%2Fneatjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphrogz%2Fneatjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphrogz%2Fneatjson/lists"}