{"id":16535423,"url":"https://github.com/hougesen/json2struct","last_synced_at":"2025-10-09T01:35:39.793Z","repository":{"id":149897424,"uuid":"615498551","full_name":"hougesen/json2struct","owner":"hougesen","description":"Easily translate JSON into type definitions. With support for TypeScript, Python, Julia and Rust","archived":false,"fork":false,"pushed_at":"2025-03-01T21:03:04.000Z","size":997,"stargazers_count":5,"open_issues_count":33,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-12T02:05:15.152Z","etag":null,"topics":["cli","json","julia","python","rust","typescript"],"latest_commit_sha":null,"homepage":"https://json2struct.mhouge.dk","language":"TypeScript","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/hougesen.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-17T20:53:54.000Z","updated_at":"2025-05-24T13:38:05.000Z","dependencies_parsed_at":"2023-11-08T11:36:30.356Z","dependency_job_id":"6d3140a7-d53b-4c7f-82c9-8b0f43018ae2","html_url":"https://github.com/hougesen/json2struct","commit_stats":{"total_commits":203,"total_committers":3,"mean_commits":67.66666666666667,"dds":"0.40394088669950734","last_synced_commit":"49028b8268696f0fb5046c14a80cc1ae1f5fc6d3"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/hougesen/json2struct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hougesen%2Fjson2struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hougesen%2Fjson2struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hougesen%2Fjson2struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hougesen%2Fjson2struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hougesen","download_url":"https://codeload.github.com/hougesen/json2struct/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hougesen%2Fjson2struct/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000720,"owners_count":26082895,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cli","json","julia","python","rust","typescript"],"created_at":"2024-10-11T18:27:25.301Z","updated_at":"2025-10-09T01:35:39.766Z","avatar_url":"https://github.com/hougesen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json2struct\n\njson2struct is a tool that translates JSON into type definitions. It currently supports translating to TypeScript, Python, Julia and Rust.\n\nThe goal is for the definitions to be used as a starting point for the user, and not as a single source of truth.\n\n## Installation\n\nThe tool can either be run using npx\n\n```sh\n$ npx json2struct \u003cREQUIRED_INPUT_FILE\u003e\n```\n\nor be installed globally\n\n```sh\n$ npm i -g json2struct\n$ json2struct \u003cREQUIRED_INPUT_FILE\u003e\n```\n\n## Usage\n\nTo use the program pass the path to the json file `$ json2struct example.json`.\n\nBy default the structure will be printed to stdout.\n\n```sh\n# example.json\n{\n    \"number_key\": 1,\n    \"string_key\": \"json2struct\",\n    \"boolean_key\": true,\n    \"array_key\": [42],\n    \"map_key\": { \"key\": \"value\" }\n}\n\n$ npx json2struct example.json\n\njson2struct: Converting example.json to typescript:\ntype GeneratedStruct = { array_key: Array\u003cnumber\u003e; boolean_key: boolean; map_key: { key: string }; number_key: number; string_key: string }\n```\n\n### Writing struct to file\n\nTo write the structure to a file pass use the output option `$ npx json2struct \u003cREQUIRED_INPUT_FILE\u003e --output \u003cOUTPUT_FILE\u003e`.\n\n```sh\n# example.json\n{\n    \"number_key\": 1,\n    \"string_key\": \"json2struct\",\n    \"boolean_key\": true,\n    \"array_key\": [42],\n    \"map_key\": { \"key\": \"value\" }\n}\n\n$ npx json2struct example.json --output example.d.ts\n```\n\nWrites the following to the output file:\n\n```typescript\n// example.d.ts\ntype GeneratedStruct = {\n    array_key: Array\u003cnumber\u003e;\n    boolean_key: boolean;\n    map_key: { key: string };\n    number_key: number;\n    string_key: string;\n};\n```\n\nThe default behavior is to append to the output file. This behavior can be overwritten by passing the option `--overwrite`.\n\n### Changing language\n\njson2struct currently supports outputting to either TypeScript or Python. By default the conversion will be to TypeScript.\n\nTo use another language pass the `--language` option.\n\n#### Python\n\n```sh\n# example.json\n{\n    \"number_key\": 1,\n    \"string_key\": \"json2struct\",\n    \"boolean_key\": true,\n    \"array_key\": [42],\n    \"map_key\": { \"key\": \"value\" }\n}\n\n$ npx json2struct example.json --output example.py --language python\n```\n\nWrites the following to the output file:\n\n```python\n# example.py\nfrom typing import List, TypedDict\n\n\nclass SubStruct1(TypedDict):\n    key: str\n\n\nclass GeneratedStruct(TypedDict):\n    array_key: List[int]\n    boolean_key: bool\n    map_key: SubStruct1\n    number_key: int\n    string_key: str\n```\n\n#### Julia\n\n```sh\n# example.json\n{\n    \"number_key\": 1,\n    \"string_key\": \"json2struct\",\n    \"boolean_key\": true,\n    \"array_key\": [42],\n    \"map_key\": { \"key\": \"value\" }\n}\n\n$ npx json2struct example.json --output example.jl --language julia\n```\n\nWrites the following to the output file:\n\n```julia\n# example.jl\nstruct SubStruct1\n    key::String\nend\n\nstruct GeneratedStruct\n    array_key::Array{Int64}\n    boolean_key::Bool\n    map_key::SubStruct1\n    number_key::Int64\n    string_key::String\nend\n```\n\n## Unknown values\n\nIf json2struct isn't able to determine the type of a given element, it tries to convert it to the closest type possible. In most languages this will be the `Any` type of the language.\n\nFor some languages, like Rust, this mean that the generated type definitions might not always be valid.\n\n### Empty arrays\n\nBy default empty arrays will be converted to:\n\n#### TypeScript\n\n```typescript\nArray\u003cunknown\u003e;\n```\n\n#### Python\n\n```python\nList[Any]\n```\n\n#### Julia\n\n```julia\nArray{Any}\n```\n\n### Empty hashmaps\n\nHashmaps without keys will be converted to:\n\n#### TypeScript\n\n```typescript\nRecord\u003cstring, unknown\u003e;\n```\n\n#### Python\n\n```python\nDict[Any, Any]\n```\n\n#### Julia\n\n```julia\nDict{Any,Any}\n```\n\n## Examples\n\nExamples of type definitions generated using json2struct can be found in the [examples folder](./examples/).\n\n## Notes\n\nAs said earlier the aim of this tool is to be used as a helper when writing type definitions, for that reason json2struct tries not to augment the output in any way.\n\nOne such example is not flattening the values of maps. In some cases it might make sense to flatten `[{ \"a\": 1 }, { \"b\": 1 }]` into `type GeneratedStruct = [{ a?: number; b?: number }];`. But in most cases flattening of maps requires having preexisting knowledge about the data that should be expected. For that reason json2struct prefers to let the user augment the type definition after, instead of imposing it's views on the user.\n\nFor the same reason json2struct does not take into account whether a key is actually valid in the given language.\n\nSince this project is mostly meant to be a way for me to familiarize myself with different languages, the types might not be the most optimal.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhougesen%2Fjson2struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhougesen%2Fjson2struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhougesen%2Fjson2struct/lists"}