json2go

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 10 Imported by: 0

README

json2go

CI Go Report Card

json2go is a tool to convert JSON to Go struct.

Installation

go install github.com/winebarrel/json2go/cmd/json2go@latest

Usage

Usage: json2go [<body-file>] [flags]

Arguments:
  [<body-file>]    JSON file. If not specified, read from stdin.

Flags:
  -h, --help       Show help.
      --version
// example.json
{
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized Markup Language",
          "Acronym": "SGML",
          "Abbrev": "ISO 8879:1986",
          "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": [
              "GML",
              "XML"
            ]
          },
          "GlossSee": "markup"
        }
      }
    }
  }
}
// json2go example.json # or `cat example.json | json2go`
struct {
	Glossary struct {
		GlossDiv struct {
			GlossList struct {
				GlossEntry struct {
					Abbrev   string `json:"Abbrev"`
					Acronym  string `json:"Acronym"`
					GlossDef struct {
						GlossSeeAlso []string `json:"GlossSeeAlso"`
						Para         string   `json:"para"`
					} `json:"GlossDef"`
					GlossSee  string `json:"GlossSee"`
					GlossTerm string `json:"GlossTerm"`
					ID        string `json:"ID"`
					SortAs    string `json:"SortAs"`
				} `json:"GlossEntry"`
			} `json:"GlossList"`
			Title string `json:"title"`
		} `json:"GlossDiv"`
		Title string `json:"title"`
	} `json:"glossary"`
}
Use as a library
package main

import (
	"fmt"
	"log"

	"github.com/winebarrel/json2go"
)

func main() {
	json := `{"foo":"bar","zoo":[100,200],"baz":{"hoge":"piyo"}}`
	gosrc, err := json2go.Convert([]byte(json))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(gosrc))
	//=> struct {
	//     Baz struct {
	//       Hoge string `json:"hoge"`
	//     } `json:"baz"`
	//     Foo string `json:"foo"`
	//     Zoo []int  `json:"zoo"`
	//   }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert added in v1.1.0

func Convert(src []byte) ([]byte, error)

Types

This section is empty.

Directories

Path Synopsis
cmd
json2go command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL