ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - m-zajac/json2go: Create go type representation from json
Create go type representation from json. Contribute to m-zajac/json2go development by creating an account on GitHub.
Visit Site

GitHub - m-zajac/json2go: Create go type representation from json

GitHub - m-zajac/json2go: Create go type representation from json

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  *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.
        } `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`.
}

CLI Installation

go get github.com/m-zajac/json2go/...

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 struct {
                Point1 Point `json:"point1"`
                Point2 Point `json:"point2"`
        } `json:"line"`
}
type Point struct {
        X float64 `json:"x"`
        Y int     `json:"y"`
}

[
    {
        "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.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory