{"id":17067752,"url":"https://github.com/rican7/conjson","last_synced_at":"2025-04-12T18:31:01.891Z","repository":{"id":57483920,"uuid":"163053725","full_name":"Rican7/conjson","owner":"Rican7","description":"(conventional, consistent, conformative) JSON - A simple, functional, no-tags-required mechanism to handle and transform JSON representations of values, consistently.","archived":false,"fork":false,"pushed_at":"2019-01-16T08:42:55.000Z","size":54,"stargazers_count":54,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T18:23:12.914Z","etag":null,"topics":["conformative","consistent","conventional","functional","go","golang","json","transform"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/Rican7/conjson","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/Rican7.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}},"created_at":"2018-12-25T06:36:55.000Z","updated_at":"2025-04-11T02:46:02.000Z","dependencies_parsed_at":"2022-08-27T21:02:17.326Z","dependency_job_id":null,"html_url":"https://github.com/Rican7/conjson","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rican7%2Fconjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rican7%2Fconjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rican7%2Fconjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rican7%2Fconjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rican7","download_url":"https://codeload.github.com/Rican7/conjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248613239,"owners_count":21133474,"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":["conformative","consistent","conventional","functional","go","golang","json","transform"],"created_at":"2024-10-14T11:11:33.363Z","updated_at":"2025-04-12T18:31:01.834Z","avatar_url":"https://github.com/Rican7.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# conjson\n\n[![Build Status](https://travis-ci.com/Rican7/conjson.svg?branch=master)](https://travis-ci.com/Rican7/conjson)\n[![Coverage Status](https://coveralls.io/repos/github/Rican7/conjson/badge.svg)](https://coveralls.io/github/Rican7/conjson)\n[![Go Report Card](https://goreportcard.com/badge/github.com/Rican7/conjson)](https://goreportcard.com/report/github.com/Rican7/conjson)\n[![GoDoc](https://godoc.org/github.com/Rican7/conjson?status.svg)](https://godoc.org/github.com/Rican7/conjson)\n[![Latest Stable Version](https://img.shields.io/github/release/Rican7/conjson.svg?style=flat)](https://github.com/Rican7/conjson/releases)\n\n**conjson** - (conventional, consistent, conformative) JSON\n\nA simple, functional, no-tags-required mechanism to handle and transform JSON representations of values, consistently.\n\n\n## Project Status\n\nThis project is currently in \"pre-release\". While the code is heavily tested, the API may change.\nVendor or \"lock\" this dependency if you plan on using it.\n\n\n## History and Motivation\n\nThis project was originally born from [a 3+ year old (at the time of creation) \"Gist\"](https://gist.github.com/Rican7/39a3dc10c1499384ca91).\n\nBoth that Gist, and eventually this larger project, were inspired by a desire to more easily work with APIs that\naccepted/returned JSON that had \"snake_case\"-style object keys.\n\nBasically, I wanted a way to Marshal and Unmarshal Go structures without having to add \"tags\" to each and every field of\neach and every structure. That Gist solved that problem for me, and now this library can do the same but with more\npower and flexibility.\n\n\n## Examples\n\n### Marshal a Go structure into \"conventional\" style JSON\n\n```go\nmodel := exampleModel{\n\tTitle:         \"Example Title\",\n\tDescription:   \"This is a description.\",\n\tImageURL:      \"https://example.com/image.png\",\n\tReferredByURL: \"https://example.com/referrer/index.html\",\n\tIsActive:      true,\n\tCreatedAt:     inceptionTime,\n\tUpdatedAt:     packageTime,\n}\n\nmarshaler := conjson.NewMarshaler(model, transform.ConventionalKeys())\n\nencoded, _ := json.MarshalIndent(marshaler, marshalPrefix, marshalIndent)\n\nfmt.Println(string(encoded))\n// Output:\n// {\n//     \"title\": \"Example Title\",\n//     \"description\": \"This is a description.\",\n//     \"image_url\": \"https://example.com/image.png\",\n//     \"referred_by_url\": \"https://example.com/referrer/index.html\",\n//     \"is_active\": true,\n//     \"created_at\": \"2015-11-17T20:43:31-05:00\",\n//     \"updated_at\": \"2018-12-24T13:21:15-07:00\"\n// }\n```\n\n### Unmarshal \"conventional\" style JSON into a Go structure\n\n```go\nsampleJSON := `\n{\n\t\"title\": \"Example Title\",\n\t\"description\": \"This is a description.\",\n\t\"image_url\": \"https://example.com/image.png\",\n\t\"referred_by_url\": \"https://example.com/referrer/index.html\",\n\t\"is_active\": true,\n\t\"created_at\": \"2015-11-17T20:43:31-05:00\",\n\t\"updated_at\": \"2018-12-24T13:21:15-07:00\"\n}\n`\n\nvar model exampleModel\n\njson.Unmarshal(\n\t[]byte(sampleJSON),\n\tconjson.NewUnmarshaler(\u0026model, transform.ConventionalKeys()),\n)\n\n// Print the \"raw\" model JSON to show result\nrawJSON, _ := json.MarshalIndent(model, marshalPrefix, marshalIndent)\nfmt.Println(string(rawJSON))\n// Output:\n// {\n//     \"Title\": \"Example Title\",\n//     \"Description\": \"This is a description.\",\n//     \"ImageURL\": \"https://example.com/image.png\",\n//     \"ReferredByURL\": \"https://example.com/referrer/index.html\",\n//     \"IsActive\": true,\n//     \"CreatedAt\": \"2015-11-17T20:43:31-05:00\",\n//     \"UpdatedAt\": \"2018-12-24T13:21:15-07:00\"\n// }\n```\n\n### Encode a Go structure into \"camelCase\" style JSON\n\n```go\nmodel := exampleModel{\n\tTitle:         \"Example Title\",\n\tDescription:   \"This is a description.\",\n\tImageURL:      \"https://example.com/image.png\",\n\tReferredByURL: \"https://example.com/referrer/index.html\",\n\tIsActive:      true,\n\tCreatedAt:     inceptionTime,\n\tUpdatedAt:     packageTime,\n}\n\njsonEncoder := json.NewEncoder(os.Stdout)\njsonEncoder.SetIndent(marshalPrefix, marshalIndent)\n\nconjson.NewEncoder(jsonEncoder, transform.CamelCaseKeys(false)).Encode(model)\n\n// Output:\n// {\n//     \"title\": \"Example Title\",\n//     \"description\": \"This is a description.\",\n//     \"imageURL\": \"https://example.com/image.png\",\n//     \"referredByURL\": \"https://example.com/referrer/index.html\",\n//     \"isActive\": true,\n//     \"createdAt\": \"2015-11-17T20:43:31-05:00\",\n//     \"updatedAt\": \"2018-12-24T13:21:15-07:00\"\n// }\n```\n\n### Decode JSON with atypical keys into a Go structure\n\n```go\nsampleJSON := `\n{\n\t\"$title--\": \"Example Title\",\n\t\"$description--\": \"This is a description.\",\n\t\"$image_url--\": \"https://example.com/image.png\",\n\t\"$referred_by_url--\": \"https://example.com/referrer/index.html\",\n\t\"$is_active--\": true,\n\t\"created_at--\": \"2015-11-17T20:43:31-05:00\",\n\t\"updated_at--\": \"2018-12-24T13:21:15-07:00\"\n}\n`\n\nvar model exampleModel\n\ndecoder := conjson.NewDecoder(\n\tjson.NewDecoder(bytes.NewBufferString(sampleJSON)),\n\ttransform.ConventionalKeys(),\n\ttransform.ValidIdentifierKeys(),\n)\n\ndecoder.Decode(\u0026model)\n\n// Print the \"raw\" model JSON to show result\nrawJSON, _ := json.MarshalIndent(model, marshalPrefix, marshalIndent)\nfmt.Println(string(rawJSON))\n// Output:\n// {\n//     \"Title\": \"Example Title\",\n//     \"Description\": \"This is a description.\",\n//     \"ImageURL\": \"https://example.com/image.png\",\n//     \"ReferredByURL\": \"https://example.com/referrer/index.html\",\n//     \"IsActive\": true,\n//     \"CreatedAt\": \"2015-11-17T20:43:31-05:00\",\n//     \"UpdatedAt\": \"2018-12-24T13:21:15-07:00\"\n// }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frican7%2Fconjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frican7%2Fconjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frican7%2Fconjson/lists"}