{"id":16985915,"url":"https://github.com/travisstaloch/json-schema-gen","last_synced_at":"2025-03-22T15:30:48.701Z","repository":{"id":246965163,"uuid":"823548583","full_name":"travisstaloch/json-schema-gen","owner":"travisstaloch","description":"Input arbitrary json. Output zig code which can parse the json.","archived":false,"fork":false,"pushed_at":"2025-03-05T21:56:51.000Z","size":58,"stargazers_count":13,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-14T18:54:33.954Z","etag":null,"topics":["json","wasm","zig"],"latest_commit_sha":null,"homepage":"https://travisstaloch.github.io/","language":"Zig","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/travisstaloch.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}},"created_at":"2024-07-03T08:38:40.000Z","updated_at":"2025-03-05T21:56:53.000Z","dependencies_parsed_at":"2024-10-28T13:23:14.474Z","dependency_job_id":null,"html_url":"https://github.com/travisstaloch/json-schema-gen","commit_stats":null,"previous_names":["travisstaloch/json-schema-gen"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisstaloch%2Fjson-schema-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisstaloch%2Fjson-schema-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisstaloch%2Fjson-schema-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/travisstaloch%2Fjson-schema-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/travisstaloch","download_url":"https://codeload.github.com/travisstaloch/json-schema-gen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244978434,"owners_count":20541854,"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":["json","wasm","zig"],"created_at":"2024-10-14T02:44:26.897Z","updated_at":"2025-03-22T15:30:48.696Z","avatar_url":"https://github.com/travisstaloch.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-schema-gen\n\nInput arbitrary json. Output zig code which can parse the json. \n\n## Web\n\nhttps://travisstaloch.github.io/\n\n## Motivation\n\nWhen you need to parse arbitrary json in zig, you usually pass `std.json.Value` to one of the parse() methods.  This is convenient but is generally slower and allocates more memory than passing a concrete type.\n\nAlso, `std.json.Value` can be a little akward at times.  Here is how it looks to access data from the [github api](https://api.github.com/search/repositories?q=topic:zig-package\u0026page=1\u0026per_page=100)\n\nwith std.json.Value:\n\n```zig\nconst url = parsed.value.object.get(\"items\").?\n    .array.items[0].object.get(\"commits_url\").?.string;\n```\nwith generated schema:\n```zig\nconst url = parsed.value.items[0].commits_url;\n```\n\n## Generate a zig schema\n\nclone or download this project and run the following, replacing `examples/1.json` with the path to your json file.\n\n```console\n$ zig build json -- examples/1.json \u003e /my/project/src/json-schema.zig\n\nparsing     .../json-schema-gen/examples/1.json\nschema file .../json-schema-gen/.zig-cache/o/1bcb06dde0b5dc7c91c6fe363f6d75fd/stdout\nsuccess!\nprinting schema to stdout\n\n$ cat /my/project/src/json-schema.zig\npub const Root = []const struct {\n    a: struct {\n        b: []const struct {\n            key: ?[]const u8 = null,\n        },\n    },\n};\n```\n\nRunning with --verbose shows how the build system uses `json-to-zig-schema.zig` to generate a zig schema file in the cache.  The schema file is then used by [src/main.zig](src/main.zig) to parse the input `examples/1.json` file.  The json path and generated schema paths are shown along with a 'success!' message or an error trace if parsing fails.\n\n```console\n$ zig build json --verbose -- examples/1.json\njson-schema-gen/.zig-cache/o/ebdbc614762cd389f778330b79addccd/json-to-zig-schema examples/1.json\n# ... omitted\n.../json-schema-gen/.zig-cache/o/0ef81ace90ad368f1e00b92e39512af1/parse-json-with-gen-schema examples/1.json\n\nparsing     .../json-schema-gen/examples/1.json\nschema file .../json-schema-gen/.zig-cache/o/1bcb06dde0b5dc7c91c6fe363f6d75fd/stdout\nsuccess!\nprinting schema to stdout\n# ... omitted\n```\n\nThe [zig script](src/json-to-zig-schema.zig) has a couple options\n\n```console\n$ zig build gen -- -h\n\nUSAGE: $ json-to-zig-schema \u003cjson-file-path\u003e \u003c?options\u003e\n  options:\n    --debug-json   - add a jsonParse() method to each object which prints field names.\n    --dump-schema  - print schema json instead of generating zig code.\n    --input-schema - treat input as schema json file and skip build phase.\n    --include-test - add a test skeleton to ouptut.\n```\n\n## Generate a zig schema from a json schema\n\n1. generate json schema (optional)\n```console\n$ zig build\n$ zig-out/bin/json-to-zig-schema examples/1.json --dump-schema \u003e /tmp/example-1-schema.json\n```\n2. specify it as input with the `--input-schema` flag\n```console\n$ zig-out/bin/json-to-zig-schema /tmp/example-1-schema.json --input-schema\npub const Root = []const struct {\n    a: struct {\n        b: []const struct {\n            key: ?[]const u8 = null,\n        },\n    },\n};\n```\n\n## Using generated schema\n\n[src/main.zig](src/main.zig) and [src/tests.zig](src/tests.zig) both show how to pass the generated schema file to a std.json.parse() method.  If you want to do the same, you can\n\n1. run `zig build json /path/to/my.json` and either redirect stdout to a file or pipe to your editor and save it next to your zig project.  lets call it `json-schema.zig`\n  * redirect to file: `zig build json -- examples/1.json \u003e /my/project/src/json-schema.zig`\n  * pipe to editor: `zig build json -- examples/1.json | nvim -`\n3. add this line a zig file where you want to use it: `const generated = @import(\"json-schema.zig\");`\n5. parse your json.\n\n## Troubleshooting parse errors\n\nIf your json file won't parse, you can use the --debug-json option to generate zig code which prints field names as they are parsed.\n\n```console\n$ zig build json -- examples/1.json --debug-json\n# ... omitted\na\nb\nkey\nsuccess!\n# ... omitted\n```\n\nHopefully that narrows your search for the problem json field and helps decide what edits to make to `json-schema.zig`.  \n\nOr if you find a way to improve the [schema gen script](src/json-to-zig-schema.zig) to fix your parse error, patches are welcome.  Please add a reproduction of your parse error to examples/ and a test case to [src/tests.zig](src/tests.zig).  You'll need to add entries for your file to `example_mods` and `example_fmts`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravisstaloch%2Fjson-schema-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftravisstaloch%2Fjson-schema-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftravisstaloch%2Fjson-schema-gen/lists"}