json2go

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 13 Imported by: 1

README

json2go Build Go Report Card GoDoc Coverage Mentioned in Awesome Go

Package json2go provides utilities for creating go type representation from json inputs.

Json2go can be used in various ways:

Why another conversion tool?

There are few tools for converting json to go types available already. But all of which I tried worked correctly with only basic documents.

The 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.

Here's the micro acid test if you want to check this or other conversion tools:

[{"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}}]

And correct output with some comments:

type Document []struct {
	Arr  [][]*float64 `json:"arr,omitempty"` // Should be doubly nested array; should be a pointer type because there's null in values.
	Bool *bool        `json:"bool,omitempty"` // Shouldn't be `bool` because when key is missing you'll get false information.
	Date *time.Time   `json:"date,omitempty"` // Could be also `string` or `*string`.
	Doc  *Doc         `json:"doc,omitempty"`  // Should be pointer, because key is not present in all documents in array.
	Doc2 *bool        `json:"_doc,omitempty"` // Attribute for "_doc" key (other that for "doc"!). Type - the same as `Bool` attribute.
	Text *string      `json:"text,omitempty"` // Could be also `string`.
}

type Doc struct {
	X *string `json:"x,omitempty"` // Should be pointer, because key is not present in all documents.
	Y *int    `json:"y,omitempty"` // Should be pointer, because key is not present in all documents.
}

CLI Installation

go install github.com/m-zajac/json2go/cmd/json2go@latest

Usage

Json2go can be used as cli tool or as package.

CLI tools can be used directly to create go type from stdin data (see examples).

Package provides Parser, which can consume multiple jsons and outputs go type fitting all inputs (see examples and documentation). Example usage: read documents from document-oriented database and feed them too parser for go struct.

CLI usage examples
echo '{"x":1,"y":2}' | json2go

curl -s https://api.punkapi.com/v2/beers?page=1&per_page=5 | json2go

Check this one :)


cat data.json | json2go
Package usage examples
inputs := []string{
	`{"x": 123, "y": "test", "z": false}`,
	`{"a": 123, "x": 12.3, "y": true}`,
}

parser := json2go.NewJSONParser("Document")
for _, in := range inputs {
	parser.FeedBytes([]byte(in))
}

res := parser.String()
fmt.Println(res)

Example outputs

{
    "line": {
        "point1": {
            "x": 12.1,
            "y": 2
        },
        "point2": {
            "x": 12.1,
            "y": 2
        }
    }
}
type Document struct {
	Line Line `json:"line"`
}

type Point struct {
	X float64 `json:"x"`
	Y int     `json:"y"`
}

type Line struct {
	Point1 Point `json:"point1"`
	Point2 Point `json:"point2"`
}

[
    {
        "name": "water",
        "type": "liquid",
        "boiling_point": {
            "units": "C",
            "value": 100
        }
    },
    {
        "name": "oxygen",
        "type": "gas",
        "density": {
            "units": "g/L",
            "value": 1.429
        }
    },
    {
        "name": "carbon monoxide",
        "type": "gas",
        "dangerous": true,
        "boiling_point": {
            "units": "C",
            "value": -191.5
        },
        "density": {
            "units": "kg/m3",
            "value": 789
        }
    }
]
type Document []struct {
	BoilingPoint *UnitsValue `json:"boiling_point,omitempty"`
	Dangerous    *bool       `json:"dangerous,omitempty"`
	Density      *UnitsValue `json:"density,omitempty"`
	Name         string      `json:"name"`
	Type         string      `json:"type"`
}

type UnitsValue struct {
	Units string  `json:"units"`
	Value float64 `json:"value"`
}

Contribution

I'd love your input! Especially reporting bugs and proposing new features.

Documentation

Overview

Package json2go implements decoding json strings to go type representation.

Index

Examples

Constants

View Source
const (
	// General options
	DefaultRootName = "Document"

	// Extraction heuristics
	DefaultSimilarityThreshold  = 0.7
	DefaultMinSubsetSize        = 2
	DefaultMinSubsetOccurrences = 2
	DefaultMinAddedFields       = 2

	// Map conversion
	DefaultMakeMaps                  = true
	DefaultMakeMapsWhenMinAttributes = 5

	// Feature toggles
	DefaultExtractAllTypes              = true
	DefaultExtractCommonTypes           = true
	DefaultStringPointersWhenKeyMissing = true
	DefaultSkipEmptyKeys                = true
	DefaultTimeAsStr                    = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONParser

type JSONParser struct {
	// contains filtered or unexported fields
}

JSONParser parses successive json inputs and returns go representation as string

func NewJSONParser

func NewJSONParser(rootTypeName string, opts ...JSONParserOpt) *JSONParser

NewJSONParser creates new json Parser

Example
inputs := []string{
	`{"line":{"start":{"x":12.1,"y":2.8},"end":{"x":12.1,"y":5.67}}}`,
	`{"triangle":[{"x":2.34,"y":2.1}, {"x":45.1,"y":6.7}, {"x":4,"y":94.6}]}`,
}

parser := NewJSONParser(
	"Document",
	OptExtractCommonTypes(true),
)

for _, in := range inputs {
	_ = parser.FeedBytes([]byte(in))
}

res := parser.String()
fmt.Println(res)
Output:
type Document struct {
	Line     *Line `json:"line,omitempty"`
	Triangle []XY  `json:"triangle,omitempty"`
}

type Line struct {
	End   XY `json:"end"`
	Start XY `json:"start"`
}

type XY struct {
	X float64 `json:"x"`
	Y float64 `json:"y"`
}

func (*JSONParser) ASTDecls

func (p *JSONParser) ASTDecls() []ast.Decl

ASTDecls returns ast type declarations

func (*JSONParser) FeedBytes

func (p *JSONParser) FeedBytes(input []byte) error

FeedBytes consumes json input as bytes. If input is invalid, json unmarshalling error is returned

func (*JSONParser) FeedValue

func (p *JSONParser) FeedValue(input any)

FeedValue consumes one of:

  • simple type (int, float, string, etc.)
  • []any - each value must meet these requirements
  • map[string]any - each value must meet these requirements

json.Unmarshal to empty interface value provides perfect input (see example)

Example
var v any
_ = json.Unmarshal([]byte(`{"line":{"start":{"x":12.1,"y":2.8},"end":{"x":12.1,"y":5.67}}}`), &v)

parser := NewJSONParser("Document")
parser.FeedValue(v)
res := parser.String()
fmt.Println(res)
Output:
type Document struct {
	Line Line `json:"line"`
}

type Line struct {
	End   XY `json:"end"`
	Start XY `json:"start"`
}

type XY struct {
	X float64 `json:"x"`
	Y float64 `json:"y"`
}

func (*JSONParser) String

func (p *JSONParser) String() string

String returns string representation of go struct fitting parsed json values

type JSONParserOpt

type JSONParserOpt func(*options)

JSONParserOpt is a type for setting parser options.

func OptExtractAllTypes added in v1.2.2

func OptExtractAllTypes(v bool) JSONParserOpt

OptExtractAllTypes toggles extracting all nested object types as separate types.

func OptExtractCommonTypes

func OptExtractCommonTypes(v bool) JSONParserOpt

OptExtractCommonTypes toggles extracting common JSON nodes as separate types.

func OptExtractHeuristics added in v1.2.0

func OptExtractHeuristics(similarity float64, minSize, minOccurrences, minAddedFields int) JSONParserOpt

OptExtractHeuristics sets thresholds for extracting similar types and shared subsets. similarity is the minimum score (0.0 to 1.0) for merging types. minSize is the minimum number of fields in a shared subset. minOccurrences is the minimum frequency of a subset to be extracted. minAddedFields is the minimum unique fields required for a new type.

func OptMakeMaps

func OptMakeMaps(v bool, minAttributes uint) JSONParserOpt

OptMakeMaps defines if the parser should attempt to use maps instead of structs when objects have similar values and can be represented as map[string]T. minAttributes defines the minimum number of attributes an object must have to be considered for conversion to a map.

func OptSkipEmptyKeys

func OptSkipEmptyKeys(v bool) JSONParserOpt

OptSkipEmptyKeys toggles skipping keys in the input that contained only null values.

func OptStringPointersWhenKeyMissing

func OptStringPointersWhenKeyMissing(v bool) JSONParserOpt

OptStringPointersWhenKeyMissing toggles whether missing string keys in some documents should result in a pointer (*string) instead of a regular string.

func OptTimeAsString

func OptTimeAsString(v bool) JSONParserOpt

OptTimeAsString toggles whether to treat valid time strings as time.Time or just as strings.

Directories

Path Synopsis
cmd
json2go command
json2go-ws command

Jump to

Keyboard shortcuts

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