{"id":13653007,"url":"https://github.com/timjansen/hanson","last_synced_at":"2026-01-10T22:06:05.464Z","repository":{"id":10047546,"uuid":"12094939","full_name":"timjansen/hanson","owner":"timjansen","description":"JSON for Humans - with unquoted identifiers, multi-line strings and comments","archived":false,"fork":false,"pushed_at":"2023-02-01T12:56:40.000Z","size":29,"stargazers_count":153,"open_issues_count":5,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-09T05:33:57.266Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timjansen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-13T22:52:24.000Z","updated_at":"2024-04-23T07:47:50.000Z","dependencies_parsed_at":"2023-02-17T05:31:31.777Z","dependency_job_id":null,"html_url":"https://github.com/timjansen/hanson","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timjansen%2Fhanson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timjansen%2Fhanson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timjansen%2Fhanson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timjansen%2Fhanson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timjansen","download_url":"https://codeload.github.com/timjansen/hanson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250384808,"owners_count":21421796,"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":[],"created_at":"2024-08-02T02:01:04.804Z","updated_at":"2025-04-23T06:31:00.336Z","avatar_url":"https://github.com/timjansen.png","language":"JavaScript","funding_links":[],"categories":["Supersets","What's Next?"],"sub_categories":["HanSON"],"readme":"HanSON - JSON for Humans\n========================\n\n*Please note (September '22)*: This project is quite old and I would suggest you check out JSON5 instead of this project. \n\nIn Short\n---------\n* HanSON is JSON with comments, multi-line strings and unquoted property names.\n* Comments use JavaScript syntax (`//`, `/**/`).\n* Supports backticks as quotes (``) for multi-line strings.\n* You can use either double-quotes (`\"\"`) or single-quotes (`''`) for single-line strings.\n* Property names do not require quotes if they are valid JavaScript identifiers.\n* Commas after the last list element or property will be ignored.\n* Every JSON string is valid HanSON.\n* HanSON can easily be converted to real JSON.\n\n\n\nIntro\n------\nJSON is a great and very simple data format, especially if you are working with JavaScript. Increasingly configuration\nfiles are written in JSON, and often it is used as a simpler alternative to XML. Unfortunately, when you are creating\nlarger JSON files by hand, you will notice some shortcomings: you need to quote all strings, even object keys;\nyou can not easily have strings with several lines; and you can not include comments.\n\nHanSON is an extension of JSON that fixes those shortcomings with a few simple additions to the JSON spec:\n* quotes for strings are optional if they follow JavaScript identifier rules.\n* you can alternatively use backticks, as in ES6's template string literal, as quotes for strings.\n  A backtick-quoted string may span several lines and you are not required to escape regular quote characters,\n  only backticks. Backslashes still need to be escaped, and all other backslash-escape sequences work like in\n  regular JSON.\n* for single-line strings, single quotes (`''`) are supported in addition to double quotes (`\"\"`)\n* you can use JavaScript comments, both single line (`//`) and multi-line comments (`/* */`), in all places where JSON allows whitespace.\n* Commas after the last list element or object property will be ignored.\n\n\n\nExample HanSON\n---------------\n```js\n{\n  listName: \"Sesame Street Monsters\", // note that listName needs no quotes\n  content: [\n    {\n      name: \"Cookie Monster\",\n      /* Note the template quotes and unescaped regular quotes in the next string */\n      background: `Cookie Monster used to be a\nmonster that ate everything, especially cookies.\nThese days he is forced to eat \"healthy\" food.`\n    }, {\n      // You can single-quote strings too:\n      name: 'Herry Monster',\n      background: `Herry Monster is a furry blue monster with a purple nose.\nHe's mostly retired today.`\n    },    // don't worry, the trailing comma will be ignored\n   ]\n}\n```\n\n\nConverting HanSON to JSON\n----------------------------\n*hanson* is a command-line converter that will convert HanSON files to JSON.\nIt is a Node.js package that can be installed using npm:\n\u003e npm install -g hanson\n\nAfter installation, convert a single file like this:\n\u003e hanson input.hson output.json\n\nYou can also convert multiple files using the -m options. It will automatically change the file extension to .json:\n\u003e hanson -m input1.hson input2.hson input3.hson input4.hson input5.hson\n\n\n\nGrunt Task to Convert HanSON to JSON\n--------------------------------------\n\nThe Grunt plugin \u003ca href=\"https://github.com/timjansen/grunt-hanson-plugin\"\u003egrunt-hanson-plugin\u003c/a\u003e can help you converting\nHanSON files to JSON. More about it in its own \u003ca href=\"https://github.com/timjansen/grunt-hanson-plugin\"\u003erepository\u003c/a\u003e.\n\n\nWebpack loader to Convert HanSON to JSON\n--------------------------------------\n\nThe Webpack loader [hson-loader](https://github.com/kentcdodds/hson-loader) can help you converting HanSON files to JSON. More\nabout it in its own [repository](https://github.com/kentcdodds/hson-loader).\n\n\nReading HanSON in JavaScript\n-------------------------------\n*hanson.js* is a simple library for Node.js that provides you with a HanSON object which works pretty much like the *JSON*\nobject, with the only difference being that `hanson.parse()` will accept HanSON.\n\n```js\nvar hanson = require('hanson');\nvar obj = hanson.parse(hansonSrc);\n```\n\n`hanson.stringify()` will currently write regular JSON and just invokes `JSON.stringify()`, but future versions may pretty-print\nthe output and use backtick quotes for multi-line strings instead of `\\n`.\n\nThere's also a `toJSON()` function that can convert your HanSON source into JSON:\n```js\nvar hanson = require('hanson');\nvar json = hanson.toJSON(hansonSrc);\n```\n\n\n\nHow Can HanSON Help Me?\n--------------------------\n* If you have configuration or descriptor files (like package.json), you can write them as HanSON and convert them\n  with the command line tool or the Grunt task.\n* Multi-line strings make it feasible to use JSON/HanSON for larger template systems, e.g. to generate static HTML pages.\n  Just write a small script that accepts HanSON and uses your favorite JavaScript template engine to create HTML.\n  Actually this is why I started HanSON - I wanted to replace my XSLT-based template system.\n* You can, of course, extend your application to accept HanSON files.\n\n\n\nFunction to Convert HanSON\n----------------------------\nWant to use HanSON in your program, without including any libraries? Use this function to convert\nHanSON to JSON. It returns a JSON string that can be read using `JSON.parse()`.\n\n```js\nfunction toJSON(input) {\n\t\tvar UNESCAPE_MAP = { '\\\\\"': '\"', \"\\\\`\": \"`\", \"\\\\'\": \"'\" };\n\t\tvar ML_ESCAPE_MAP = {'\\n': '\\\\n', \"\\r\": '\\\\r', \"\\t\": '\\\\t', '\"': '\\\\\"'};\n\t\tfunction unescapeQuotes(r) { return UNESCAPE_MAP[r] || r; }\n\n\t\treturn input.replace(/`(?:\\\\.|[^`])*`|'(?:\\\\.|[^'])*'|\"(?:\\\\.|[^\"])*\"|\\/\\*[^]*?\\*\\/|\\/\\/.*\\n?/g, // pass 1: remove comments\n\t\t\t\t\t\t\t function(s) {\n\t\t\tif (s.charAt(0) == '/')\n\t\t\t\treturn '';\n\t\t\telse  \n\t\t\t\treturn s;\n\t\t})\n\t\t.replace(/(?:true|false|null)(?=[^\\w_$]|$)|([a-zA-Z_$][\\w_$]*)|`((?:\\\\.|[^`])*)`|'((?:\\\\.|[^'])*)'|\"(?:\\\\.|[^\"])*\"|(,)(?=\\s*[}\\]])/g, // pass 2: requote\n\t\t\t\t\t\t\t function(s, identifier, multilineQuote, singleQuote, lonelyComma) {\n\t\t\tif (lonelyComma)\n\t\t\t\treturn '';\n\t\t\telse if (identifier != null)\n\t\t\t\t\treturn '\"' + identifier + '\"';\n\t\t\telse if (multilineQuote != null)\n\t\t\t\treturn '\"' + multilineQuote.replace(/\\\\./g, unescapeQuotes).replace(/[\\n\\r\\t\"]/g, function(r) { return ML_ESCAPE_MAP[r]; }) + '\"';\n\t\t\telse if (singleQuote != null)\n\t\t\t\treturn '\"' + singleQuote.replace(/\\\\./g, unescapeQuotes).replace(/\"/g, '\\\\\"') + '\"';\n\t\t\telse\n\t\t\t\treturn s;\n\t\t});\n}\n```\n\nChanges\n--------\n* August 14, 2013: First release (0.1.0)\n* August 15, 2013: Replaced triple-quotes with backticks (1.0.0, backward-incompatible change)\n* August 19, 2013: Added support for single-quotes (1.1.0)\n* June 14, 2016: Support for STDIN/STDOUT in command line. Improved tests. Thank you, Matt Carter (1.2.0)\n\n\nLicense\n--------\nAll code and documentation has been dedicated to the public domain:\nhttp://unlicense.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimjansen%2Fhanson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimjansen%2Fhanson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimjansen%2Fhanson/lists"}