{"id":24558110,"url":"https://github.com/rse/json-asty","last_synced_at":"2025-04-19T09:58:52.909Z","repository":{"id":46225851,"uuid":"126825331","full_name":"rse/json-asty","owner":"rse","description":"Lossless JSON-to-AST Parser and AST-to-JSON Generator","archived":false,"fork":false,"pushed_at":"2024-06-24T19:49:57.000Z","size":73,"stargazers_count":38,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T00:43:01.425Z","etag":null,"topics":["ast","generator","json","lossless","parser"],"latest_commit_sha":null,"homepage":"http://npmjs.com/json-asty","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-03-26T12:28:34.000Z","updated_at":"2025-02-01T18:55:35.000Z","dependencies_parsed_at":"2024-03-08T23:49:01.607Z","dependency_job_id":null,"html_url":"https://github.com/rse/json-asty","commit_stats":{"total_commits":59,"total_committers":1,"mean_commits":59.0,"dds":0.0,"last_synced_commit":"019397d9443192a2d5236042f998f3aba6d00893"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fjson-asty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fjson-asty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fjson-asty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fjson-asty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rse","download_url":"https://codeload.github.com/rse/json-asty/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248270035,"owners_count":21075790,"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":["ast","generator","json","lossless","parser"],"created_at":"2025-01-23T05:47:25.259Z","updated_at":"2025-04-19T09:58:52.891Z","avatar_url":"https://github.com/rse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nJSON-ASTy\n=========\n\nLossless JSON-to-AST Parser and AST-to-JSON Generator\n\n[![github (author stars)](https://img.shields.io/github/stars/rse?logo=github\u0026label=author%20stars\u0026color=%233377aa)](https://github.com/rse)\n[![github (author followers)](https://img.shields.io/github/followers/rse?label=author%20followers\u0026logo=github\u0026color=%234477aa)](https://github.com/rse)\n\u003cbr/\u003e\n[![npm (project release)](https://img.shields.io/npm/v/json-asty?logo=npm\u0026label=npm%20release\u0026color=%23cc3333)](https://npmjs.com/json-asty)\n[![npm (project downloads)](https://img.shields.io/npm/dm/json-asty?logo=npm\u0026label=npm%20downloads\u0026color=%23cc3333)](https://npmjs.com/json-asty)\n\nAbout\n-----\n\nJSON-ASTy is a JavaScript library providing a lossless JavaScript Object\nNotation (JSON) to Abstract Syntax Tree (AST) parser and a corresponding\nAST to JSON generator. It is intended for cases where one has to read\nJSON into an AST, manipulate the AST and generate JSON from the AST\nagain while fully preserving the formatting of the original JSON.\nThe AST is based on [ASTy-ASTq](http://npmjs.com/asty-astq), and\nhence can powerfully queried with [ASTq](http://npmjs.com/astq), and\nmanipulated with [ASTy](http://npmjs.com/asty).\n\nInstallation\n------------\n\n```shell\n$ npm install json-asty\n```\n\nUsage\n-----\n\n```js\nconst JsonAsty = require(\"json-asty\")\n\n/*  the JSON input  */\nlet json = `{\n    \"foo\": {\n        \"bar\": true,\n        \"baz\": 42.0,\n        \"quux\": [ \"test1\\\\\"test2\", \"test3\", 7, true ]\n    }\n}`\nconsole.log(`JSON (old):\\n${json}`)\n\n/*  parse JSON into AST  */\nlet ast = JsonAsty.parse(json)\nconsole.log(`AST Dump (all):\\n${JsonAsty.dump(ast, { colors: true })}`)\n\n/*  the AST query  */\nlet query = `\n    .// member [\n        ..// member [\n            / string [ pos() == 1 \u0026\u0026 @value == \"foo\" ]\n        ]\n        \u0026\u0026\n        / string [ pos() == 1 \u0026\u0026 @value == \"baz\" ]\n    ]\n        / * [ pos() == 2 ]\n`\nconsole.log(`AST Query:\\n${query}`)\n\n/*  query AST node  */\nlet nodes = ast.query(query)\nlet node = nodes[0]\nconsole.log(`AST Dump (sub, old):\\n${node.dump()}`)\n\n/*  manipulate AST node  */\nlet nodeNew = node.create(\"string\").set({ value: \"TEST\" })\nnode.parent().del(node).add(nodeNew)\nconsole.log(`AST Dump (sub, new):\\n${node.dump()}`)\n\n/*  unparse AST into JSON  */\nlet jsonNew = JsonAsty.unparse(ast)\nconsole.log(`JSON (new):\\n${jsonNew}`)\n```\n\nOutput:\n\n```\nJSON (old):\n{\n    \"foo\": {\n        \"bar\": true,\n        \"baz\": 42.0,\n        \"quux\": [ \"test1\\\"test2\", \"test3\", 7, true ]\n    }\n}\nAST Dump (all):\nobject (prolog: \"{\\n    \", epilog: \"}\") [1,1]\n└── member [2,5]\n    ├── string (body: \"\\\"foo\\\"\", value: \"foo\", epilog: \": \") [2,5]\n    └── object (prolog: \"{\\n        \", epilog: \"}\\n\") [2,12]\n        ├── member (epilog: \",\\n        \") [3,9]\n        │   ├── string (body: \"\\\"bar\\\"\", value: \"bar\", epilog: \": \") [3,9]\n        │   └── boolean (body: \"true\", value: true) [3,16]\n        ├── member (epilog: \",\\n        \") [4,9]\n        │   ├── string (body: \"\\\"baz\\\"\", value: \"baz\", epilog: \": \") [4,9]\n        │   └── number (body: \"42.0\", value: 42) [4,16]\n        └── member [5,9]\n            ├── string (body: \"\\\"quux\\\"\", value: \"quux\", epilog: \": \") [5,9]\n            └── array (prolog: \"[ \", epilog: \" ]\\n    \") [5,17]\n                ├── string (body: \"\\\"test1\\\\\\\"test2\\\"\", value: \"test1\\\"test2\", epilog: \", \") [5,19]\n                ├── string (body: \"\\\"test3\\\"\", value: \"test3\", epilog: \", \") [5,35]\n                ├── number (body: \"7\", value: 7, epilog: \", \") [5,44]\n                └── boolean (body: \"true\", value: true) [5,47]\n\nAST Query:\n\n    .// member [\n        ..// member [\n            / string [ pos() == 1 \u0026\u0026 @value == \"foo\" ]\n        ]\n        \u0026\u0026\n        / string [ pos() == 1 \u0026\u0026 @value == \"baz\" ]\n    ]\n        / * [ pos() == 2 ]\n\nAST Dump (sub, old):\nnumber (body: \"42.0\", value: 42) [4,16]\n\nAST Dump (sub, new):\nnumber (body: \"42.0\", value: 42) [4,16]\n\nJSON (new):\n{\n    \"foo\": {\n        \"bar\": true,\n        \"baz\": \"TEST\",\n        \"quux\": [ \"test1\\\"test2\", \"test3\", 7, true ]\n    }\n}\n```\n\nLicense\n-------\n\nCopyright \u0026copy; 2018-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fjson-asty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frse%2Fjson-asty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fjson-asty/lists"}