{"id":13786770,"url":"https://m-zajac.github.io/json2go/","last_synced_at":"2025-05-11T22:33:30.797Z","repository":{"id":49798633,"uuid":"93971714","full_name":"m-zajac/json2go","owner":"m-zajac","description":"Create go type representation from json","archived":false,"fork":false,"pushed_at":"2021-12-15T12:21:53.000Z","size":5583,"stargazers_count":127,"open_issues_count":1,"forks_count":17,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-19T17:38:02.067Z","etag":null,"topics":["cli","json","json-to-go","json-to-golang","json-to-struct"],"latest_commit_sha":null,"homepage":"https://m-zajac.github.io/json2go","language":"Go","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/m-zajac.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}},"created_at":"2017-06-10T23:55:07.000Z","updated_at":"2024-03-14T07:19:51.000Z","dependencies_parsed_at":"2022-08-12T20:40:32.066Z","dependency_job_id":null,"html_url":"https://github.com/m-zajac/json2go","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-zajac%2Fjson2go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-zajac%2Fjson2go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-zajac%2Fjson2go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-zajac%2Fjson2go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m-zajac","download_url":"https://codeload.github.com/m-zajac/json2go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225105772,"owners_count":17421791,"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":["cli","json","json-to-go","json-to-golang","json-to-struct"],"created_at":"2024-08-03T19:01:35.010Z","updated_at":"2024-11-17T23:30:26.183Z","avatar_url":"https://github.com/m-zajac.png","language":"Go","funding_links":[],"categories":["Websites"],"sub_categories":["Reddit"],"readme":"# json2go ![Build](https://github.com/m-zajac/json2go/workflows/Build/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/m-zajac/json2go)](https://goreportcard.com/report/github.com/m-zajac/json2go) [![GoDoc](https://godoc.org/github.com/m-zajac/json2go?status.svg)](http://godoc.org/github.com/m-zajac/json2go) [![Coverage](https://img.shields.io/badge/coverage-gocover.io-blue)](https://gocover.io/github.com/m-zajac/json2go) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\nPackage json2go provides utilities for creating go type representation from json inputs.\n\nJson2go can be used in various ways:\n\n- CLI tool\n- Web page with conversion tool: [https://m-zajac.github.io/json2go](https://m-zajac.github.io/json2go)\n- Go package\n- VSCode extension: [vsc-json2go](https://marketplace.visualstudio.com/items?itemName=m-zajac.vsc-json2go)\n\n## Why another conversion tool?\n\nThere are few tools for converting json to go types available already. But all of which I tried worked correctly with only basic documents. \n\nThe goal of this project is to create types, that are guaranteed to properly unmarshal input data. There are multiple test cases that check marshaling/unmarshaling both ways to ensure generated type is accurate.\n\nHere's the micro acid test if you want to check this or other conversion tools:\n\n```json\n[{\"date\":\"2020-10-03T15:04:05Z\",\"text\":\"txt1\",\"doc\":{\"x\":\"x\"}},{\"date\":\"2020-10-03T15:05:02Z\",\"_doc\":false,\"arr\":[[1,null,1.23]]},{\"bool\":true,\"doc\":{\"y\":123}}]\n```\n\nAnd correct output with some comments:\n\n```go\ntype Document []struct {\n        Arr  [][]*float64 `json:\"arr,omitempty\"` // Should be doubly nested array; should be a pointer type because there's null in values.\n        Bool *bool        `json:\"bool,omitempty\"` // Shouldn't be `bool` because when key is missing you'll get false information.\n        Date *time.Time   `json:\"date,omitempty\"` // Could be also `string` or `*string`.\n        Doc  *struct {\n                X *string `json:\"x,omitempty\"` // Should be pointer, because key is not present in all documents.\n                Y *int    `json:\"y,omitempty\"` // Should be pointer, because key is not present in all documents.\n        } `json:\"doc,omitempty\"` // Should be pointer, because key is not present in all documents in array.\n        Doc2 *bool   `json:\"_doc,omitempty\"` // Attribute for \"_doc\" key (other that for \"doc\"!). Type - the same as `Bool` attribute.\n        Text *string `json:\"text,omitempty\"` // Could be also `string`.\n}\n```\n\n## CLI Installation\n\n    go get github.com/m-zajac/json2go/...\n\n## Usage\n\nJson2go can be used as cli tool or as package.\n\nCLI tools can be used directly to create go type from stdin data (see examples).\n\nPackage provides Parser, which can consume multiple jsons and outputs go type fitting all inputs (see examples and [documentation](https://godoc.org/github.com/m-zajac/json2go)). Example usage: read documents from document-oriented database and feed them too parser for go struct.\n\n### CLI usage examples\n\n    echo '{\"x\":1,\"y\":2}' | json2go\n\n---\n\n    curl -s https://api.punkapi.com/v2/beers?page=1\u0026per_page=5 | json2go\n\nCheck this one :)\n\n---\n\n    cat data.json | json2go\n\n### Package usage examples\n\n```go\ninputs := []string{\n\t`{\"x\": 123, \"y\": \"test\", \"z\": false}`,\n\t`{\"a\": 123, \"x\": 12.3, \"y\": true}`,\n}\n\nparser := json2go.NewJSONParser(\"Document\")\nfor _, in := range inputs {\n\tparser.FeedBytes([]byte(in))\n}\n\nres := parser.String()\nfmt.Println(res)\n```\n\n## Example outputs\n\n```json\n{\n    \"line\": {\n        \"point1\": {\n            \"x\": 12.1,\n            \"y\": 2\n        },\n        \"point2\": {\n            \"x\": 12.1,\n            \"y\": 2\n        }\n    }\n}\n```\n```go\ntype Document struct {\n        Line struct {\n                Point1 Point `json:\"point1\"`\n                Point2 Point `json:\"point2\"`\n        } `json:\"line\"`\n}\ntype Point struct {\n        X float64 `json:\"x\"`\n        Y int     `json:\"y\"`\n}\n```\n\n---\n\n```json\n[\n    {\n        \"name\": \"water\",\n        \"type\": \"liquid\",\n        \"boiling_point\": {\n            \"units\": \"C\",\n            \"value\": 100\n        }\n    },\n    {\n        \"name\": \"oxygen\",\n        \"type\": \"gas\",\n        \"density\": {\n            \"units\": \"g/L\",\n            \"value\": 1.429\n        }\n    },\n    {\n        \"name\": \"carbon monoxide\",\n        \"type\": \"gas\",\n        \"dangerous\": true,\n        \"boiling_point\": {\n            \"units\": \"C\",\n            \"value\": -191.5\n        },\n        \"density\": {\n            \"units\": \"kg/m3\",\n            \"value\": 789\n        }\n    }\n]\n```\n```go\ntype Document []struct {\n        BoilingPoint *UnitsValue `json:\"boiling_point,omitempty\"`\n        Dangerous    *bool       `json:\"dangerous,omitempty\"`\n        Density      *UnitsValue `json:\"density,omitempty\"`\n        Name         string      `json:\"name\"`\n        Type         string      `json:\"type\"`\n}\ntype UnitsValue struct {\n        Units string  `json:\"units\"`\n        Value float64 `json:\"value\"`\n}\n```\n\n## Contribution\n\nI'd love your input! Especially reporting bugs and proposing new features.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/m-zajac.github.io%2Fjson2go%2F","html_url":"https://awesome.ecosyste.ms/projects/m-zajac.github.io%2Fjson2go%2F","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/m-zajac.github.io%2Fjson2go%2F/lists"}