{"id":13613810,"url":"https://github.com/twpayne/go-jsonstruct","last_synced_at":"2025-05-16T00:03:34.887Z","repository":{"id":42989841,"uuid":"187244660","full_name":"twpayne/go-jsonstruct","owner":"twpayne","description":"Generate Go structs from multiple JSON or YAML objects.","archived":false,"fork":false,"pushed_at":"2025-03-29T11:14:50.000Z","size":105,"stargazers_count":336,"open_issues_count":0,"forks_count":34,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-14T03:09:12.803Z","etag":null,"topics":["go","golang","json","json-schema","schema","struct","yaml"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twpayne.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":"2019-05-17T15:57:11.000Z","updated_at":"2025-05-03T21:43:50.000Z","dependencies_parsed_at":"2024-06-18T15:33:23.918Z","dependency_job_id":"2abf8a7b-38d7-4525-b28e-b361d18886e1","html_url":"https://github.com/twpayne/go-jsonstruct","commit_stats":{"total_commits":73,"total_committers":4,"mean_commits":18.25,"dds":0.09589041095890416,"last_synced_commit":"e8bee4944e3a49590df058ec52b953e8d88a2fbc"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twpayne%2Fgo-jsonstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twpayne%2Fgo-jsonstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twpayne%2Fgo-jsonstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twpayne%2Fgo-jsonstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twpayne","download_url":"https://codeload.github.com/twpayne/go-jsonstruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442854,"owners_count":22071877,"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":["go","golang","json","json-schema","schema","struct","yaml"],"created_at":"2024-08-01T20:00:53.814Z","updated_at":"2025-05-16T00:03:33.355Z","avatar_url":"https://github.com/twpayne.png","language":"Go","readme":"# go-jsonstruct\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/twpayne/go-jsonstruct/v3)](https://pkg.go.dev/github.com/twpayne/go-jsonstruct/v3)\n\nGenerate Go structs from multiple JSON or YAML objects.\n\n- [go-jsonstruct](#go-jsonstruct)\n  - [What does go-jsonstruct do and why should I use it?](#what-does-go-jsonstruct-do-and-why-should-i-use-it)\n  - [How do I use go-jsonstruct?](#how-do-i-use-go-jsonstruct)\n  - [YAML support](#yaml-support)\n  - [What are go-jsonstruct's key features?](#what-are-go-jsonstructs-key-features)\n  - [How does go-jsonstruct work?](#how-does-go-jsonstruct-work)\n  - [License](#license)\n\n## What does go-jsonstruct do and why should I use it?\n\ngo-jsonstruct generates Go structs from *multiple* JSON or YAML objects.\nExisting Go struct generators such as\n[json-to-go](https://mholt.github.io/json-to-go/) and\n[json2struct](http://json2struct.mervine.net/) take only a *single* JSON object\nas input. go-jsonstruct takes multiple objects as input and generates the most\nspecific Go struct possible into which all the input objects can be\nunmarshalled.\n\nThis is useful if you have a collection of JSON objects, where no single object\nhas all properties present, and you want to unmarshal those JSON objects into a\nGo program. Example collections include:\n\n* JSON responses received from a REST API with no defined schema.\n* Multiple values from a JSON column in an SQL database.\n* All the JSON documents in a document database.\n* All the YAML configuration files in a directory.\n\n## How do I use go-jsonstruct?\n\nInstall go-jsonstruct:\n\n```sh\ngo install github.com/twpayne/go-jsonstruct/v3/cmd/gojsonstruct@latest\n```\n\nFeed it some JSON objects. For example you can feed it with\n\n```json\n{\n  \"age\": 37,\n  \"user_height_m\": 2\n}\n\n{\n  \"age\": 38,\n  \"user_height_m\": 1.7,\n  \"favoriteFoods\": [\n    \"cake\"\n  ]\n}\n```\n\nby running\n\n```sh\necho '{\"age\":37,\"user_height_m\":2}' \\\n    '{\"age\":38,\"user_height_m\":1.7,\"favoriteFoods\":[\"cake\"]}' \\\n    | gojsonstruct\n```\n\nThis will output:\n\n```go\npackage main\n\ntype T struct {\n    Age           int      `json:\"age\"`\n    FavoriteFoods []string `json:\"favoriteFoods,omitempty\"`\n    UserHeightM   float64  `json:\"user_height_m\"`\n}\n```\n\nThis example demonstrates:\n\n* `age` is always observed as an integer, and so is given type `int`. The\n  lower-case JSON property `age` is converted into an exported Go field name\n  `Age` for compatibility with `encoding/json`.\n* `favoriteFoods` is observed as a JSON array, but is not always present, but\n  when it is present it only contains JSON strings, and so is given type\n  `[]string`. The `camelCase` name `favoriteFoods` is converted into the\n  exported Go field name `FavoriteFoods` and is tagged with `omitempty`.\n* `user_height_m` is observed as JSON numbers `2` and `1.7`, for which the most\n  specific Go type is `float64`. The `snake_case` name `user_height_m` is\n  converted to the exported Go field name `UserHeightM`.\n* Properties are sorted alphabetically.\n\ngo-jsonstruct recursively handles nested array elements and objects. For\nexample, given the following three JSON objects input:\n\n```json\n{\n  \"nested\": {\n    \"bar\": true,\n    \"foo\": \"baz\"\n  }\n}\n\n{\n  \"nested\": {\n    \"bar\": false,\n    \"foo\": null\n  }\n}\n\n{\n  \"nested\": {\n    \"bar\": true,\n    \"foo\": \"\"\n  }\n}\n```\n\nwhich you can try by running\n\n```sh\necho '{\"nested\":{\"bar\":true,\"foo\":\"baz\"}}' \\\n    '{\"nested\":{\"bar\":false,\"foo\":null}}' \\\n    '{\"nested\":{\"bar\":true,\"foo\":\"\"}}' \\\n    | gojsonstruct --package-name mypackage --typename MyType\n```\n\ngenerates the output\n\n```go\npackage mypackage\n\ntype MyType struct {\n    Nested struct {\n        Bar bool    `json:\"bar\"`\n        Foo *string `json:\"foo\"`\n    } `json:\"nested\"`\n}\n```\n\nThis demonstrates:\n\n* The package name and type name can be set on the command line.\n* The arbitrarily-complex property `nested` is recursively converted to a nested\n  `struct` that all values can be unmarshalled to. go-jsonstruct handles array\n  elements in an identical fashion, resolving array elements to the\n  most-specific type.\n* `nested.bar` is always observed as a JSON bool, and is converted to a field of\n  type `bool`.\n* `nested.foo` is observed as a JSON string, a JSON null, and an empty JSON\n  string and is converted to a field of type `*string` without `omitempty`. With\n  `omitempty`, Go's `encoding/json` omits the field in the two cases of  `nil`\n  and a pointer to an empty `string`, so there is no way to distinguish between\n  the observed values of `null` and `\"\"`. go-jsonstruct falls back to the option\n  of `*string` without `omitempty` which means that a value is always present,\n  even if empty.\n\nYou can feed it your own data via the standard input, for example if you have a\nfile with one JSON object per line in `objects.json` you can run:\n\n    gojsonstruct \u003c objects.json\n\nTo learn about more about the available options, run:\n\n    gojsonstruct --help\n\n## YAML support\n\nFor YAML files, pass the `--format=yaml` flag, for example:\n\n```console\n$ gojsonstruct --format=yaml *.yaml\n```\n\ngojsonstruct will analyze all passed YAML files and generate a Go struct with\n`yaml:\"...\"` struct tags.\n\n## What are go-jsonstruct's key features?\n\n* Finds the most specific Go type that can represent all input values.\n* Handles JSON and YAML.\n* Generates Go struct field names from  `camelCase`, `kebab-case`, and\n  `snake_case` object property names.\n* Capitalizes common abbreviations (e.g. HTTP, ID, and URL) when\n  generating Go struct field names to follow Go conventions, with the option to\n  add your own abbreviations.\n* Gives you control over the output, including the generated package name, type\n  name, and godoc-compatible comments.\n* Generates deterministic output based only on the determined structure of the\n  input, making it suitable for incorporation into build pipelines or detecting\n  schema changes.\n* Generates `,omitempty` tags.\n* Generates `,omitzero` tags.\n* Generates `,string` tags.\n* Uses the standard library's `time.Time` when possible.\n* Gracefully handles properties with spaces that [cannot be unmarshalled by\n  `encoding/json`](https://github.com/golang/go/issues/18531).\n\n## How does go-jsonstruct work?\n\ngo-jsonstruct consists of two phases: observation and code generation.\n\nFirstly, in the observation phase, go-jsonstruct explores all the input objects\nand records statistics on what types are observed in each part. It recurses into\nobjects and iterates over arrays.\n\nSecondly, in the code generation phase, go-jsonstruct inspects the gathered\nstatistics and determines the strictest possible Go type that can represent all\nthe observed values. For example, the values `0` and `1` can be represented as\nan `int`, the values `0`, `1`, and `2.2` require a `float64`, and `true`, `3.3`,\nand `\"name\"` require an `any`.\n\n## License\n\nBSD\n","funding_links":[],"categories":["Go","Dev-Tools"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwpayne%2Fgo-jsonstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwpayne%2Fgo-jsonstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwpayne%2Fgo-jsonstruct/lists"}