{"id":29360023,"url":"https://github.com/bbredesen/mjson2go","last_synced_at":"2025-07-16T22:38:05.080Z","repository":{"id":38345240,"uuid":"356683059","full_name":"bbredesen/mjson2go","owner":"bbredesen","description":"A tool to convert a MongoDB pipeline into parameterized Go code.","archived":false,"fork":false,"pushed_at":"2024-02-05T14:57:10.000Z","size":2688,"stargazers_count":25,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-09T07:09:08.946Z","etag":null,"topics":["code-generator","go","golang","mongodb"],"latest_commit_sha":null,"homepage":"","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/bbredesen.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":"2021-04-10T20:06:47.000Z","updated_at":"2025-07-07T22:21:42.000Z","dependencies_parsed_at":"2024-06-19T17:52:43.295Z","dependency_job_id":null,"html_url":"https://github.com/bbredesen/mjson2go","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/bbredesen/mjson2go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbredesen%2Fmjson2go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbredesen%2Fmjson2go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbredesen%2Fmjson2go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbredesen%2Fmjson2go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbredesen","download_url":"https://codeload.github.com/bbredesen/mjson2go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbredesen%2Fmjson2go/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264411163,"owners_count":23603804,"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":["code-generator","go","golang","mongodb"],"created_at":"2025-07-09T07:09:01.333Z","updated_at":"2025-07-09T07:09:54.211Z","avatar_url":"https://github.com/bbredesen.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mjson2go\n\n`mjson2go` is a tool that generates parameterized Go code usable by the MongoDB driver from a JSON pipeline source.\n\n### [Try it in your browser](https://bbredesen.github.io/mjson2go)\n\nDeveloping a MongoDB pipeline in Go is awkward, with lots of double braces and nested `bson.D` or `bson.A`s. Here is a very simple aggregation pipeline in JSON:\n\n```json\n[\n    {\n        \"$match\" : {\n            \"postType\" : \"Article\"\n        }\n    },\n    {\n        \"$group\" : {\n            \"_id\" : { \"user\" : \"$userId\" },\n            \"allPosts\": { \"$push\" : \"$$ROOT\" },\n            \"count\" : { \"$sum\" : 1 }\n        }\n    }\n]\n```\n\nTranslated to Go, this becomes:\n\n```go\npipeline := bson.A{\n    bson.D{\n        { \"$match\", bson.D{\n            { \"postType\", \"Article\" }\n        },\n    }},\n    bson.D{\n        { \"$group\", bson.D{\n                { \"_id\", bson.D{\n                    {\"user\", \"$userId\"}\n                }},\n                {\"allPosts\", bson.D{\n                    {\"$push\", \"$$ROOT\"}\n                }},\n                {\"count\", bson.D{\n                    {\"$sum\", 1}\n                }},\n        }\n    }},\n}\n```\n\n`mjson2go` allows you to export an aggregation pipeline from MongoDB Compass and/or directly develop your pipeline in JSON, instead of trying to copy and paste, translate into Go source, correct syntax, match braces, etc. etc.\n\n`mjson2go` can be used from the command line or, ideally, via `go generate`. The tool produces a Go source file with a \"Get___\" function that returns a `bson.D` or `bson.A`, and allows you to specify input parameters from your code.\n\nFinally, the tool will format and run `goimports` on the resulting source file.\n\n# Usage\n\n```\ngo install github.com/bbredesen/mjson2go@latest\n```\n\n## Via the Command Line\n```\nmjson2go -out=pipeline.go aggregation.json otherAggregation.json\n```\n\nThe command above will produce Go source containing functions `GetAggregation()` and `GetOtherAggregation()`. If no files are provided, it will read from stdin and write to stdout. If a directory name is provided, it will process all files ending in .json in that directory.\n\n## Via `go generate`\nThe tool will automatically put the resulting code in the same package as the `go:generate` annotation:\n\n```go\npackage somepkg\n//go:generate mjson2go -out=pipeline.go aggregations/\n```\n\nNote that `go generate` does not pass commands through a shell and will not expand globs.\n\n# Pipeline Parameters\n\nInput parameters can be specified as a string in your JSON file prefixed with \"%%\", and optionally with a Go type specification and ordering for the function arguments: \n```json\n{\n    \"index\": \"%%intParam%int%2\",\n    \"date\": \"%%dateParam%time.Time%1\"\n}\n```\n\nThis will produce a function similar to \n```go\nfunc GetAggregation(dateParam time.Time, intParam int) bson.D {\n    // ... \n}\n```\n\n## Field specification\nAll parts of the parameter specifier are optional, including the parameter name. You can simply write \"%%\" to create a parameter with all default values. Parameter names can be repeated to reuse the same value in the output.\n\n`\"%%\u003cname\u003e%\u003ctype\u003e%\u003corder\u003e\"`\n\n`\u003cname\u003e` - The parameter name. Defaults to p\\\u003cindex\\\u003e (p0, p1, etc.). Note that the tool will not stop you from using a Go keyword as a name.\n\n`\u003ctype\u003e` - The Go type specification. Defaults to string. The tool will run goimports after code generation and attempt to import non-primitive types.\n\n`\u003corder\u003e` - Numeric key for the order of paramters in the function specification. Need not be sequential. Parameters default to source order, with the caveat that all explicitly ordered parameters are added first.\n\nUsage examples: \n\n- `\"%%articleType\"` (defaults to string)\n- `\"%%userId%int\"`\n- `\"%%beforeDate%time.Time\"`\n- `\"%%mongoKey%primitive.ObjectID\"`\n- `\"%%\"` (defaults to string with a generated name)\n\n## Command Line Flags\n\n### `-fix`\nCorrects three common JSON syntax errors that Compass (and Javascript) may allow, but which will cause unexpected JSON parsing behavior with this tool. \n\n* Corrects un-quoted object keys, i.e., changes `$match` to `\"$match\"`\n* Change single-quote strings to double quotes\n* Remove trailing commas after the last element of an array or object\n\n`-fix` also formats and indents the resulting source with two spaces, and overwrites the original source file.\n\nDefaults to true, so you will have to explicitly pass -fix=false to not fix (potential) source errors.\n\n###  `-keys`\nThe generated bson.D structs will used keyed field names (`Key:` and `Value:`) in the output. Unkeyed struct\ninitializers are legal syntax and (in this context \u0026 in my opinion) are easier to read, but many linters complain about\nit. Defaults to true, so you will have to explicitly pass -keys=false to generate unkeyed initializers. \n\nOption added in v0.3.0 and changes the default behavior of the tool. (Previous versions of mjson2go only generated unkeyed literals.)\n\n### `-package=pkgname`\nBy default, the generated code is in the main package or the value of the `$GOPACAKGE` environment variable (which is set by `go generate`). Setting this flag will override the default name.\n\n### `-v`\n(Slightly) more verbose output to `stderr`.\n\n### `-out=filename.go`\nWrite the output to the provided filename. By default, output goes to `stdout` on the command line, or into \n`\u003ccurrent file\u003e_mjson.go` if called via `go generate`. NOTE: `goimports` will not be run if output goes to \n`stdout`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbredesen%2Fmjson2go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbredesen%2Fmjson2go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbredesen%2Fmjson2go/lists"}