Automatically generate Go (golang) struct definitions from example JSON

Overview

Build Status gojson

gojson generates go struct definitions from json or yaml documents.

Example

$ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository

package main

type Repository struct {
	ArchiveURL       string      `json:"archive_url"`
	AssigneesURL     string      `json:"assignees_url"`
	BlobsURL         string      `json:"blobs_url"`
	BranchesURL      string      `json:"branches_url"`
	CloneURL         string      `json:"clone_url"`
	CollaboratorsURL string      `json:"collaborators_url"`
	CommentsURL      string      `json:"comments_url"`
	CommitsURL       string      `json:"commits_url"`
	CompareURL       string      `json:"compare_url"`
	ContentsURL      string      `json:"contents_url"`
	ContributorsURL  string      `json:"contributors_url"`
	CreatedAt        string      `json:"created_at"`
	DefaultBranch    string      `json:"default_branch"`
	Description      string      `json:"description"`
	DownloadsURL     string      `json:"downloads_url"`
	EventsURL        string      `json:"events_url"`
	Fork             bool        `json:"fork"`
	Forks            float64     `json:"forks"`
	ForksCount       float64     `json:"forks_count"`
	ForksURL         string      `json:"forks_url"`
	FullName         string      `json:"full_name"`
	GitCommitsURL    string      `json:"git_commits_url"`
	GitRefsURL       string      `json:"git_refs_url"`
	GitTagsURL       string      `json:"git_tags_url"`
	GitURL           string      `json:"git_url"`
	HasDownloads     bool        `json:"has_downloads"`
	HasIssues        bool        `json:"has_issues"`
	HasWiki          bool        `json:"has_wiki"`
	Homepage         interface{} `json:"homepage"`
	HooksURL         string      `json:"hooks_url"`
	HtmlURL          string      `json:"html_url"`
	ID               float64     `json:"id"`
	IssueCommentURL  string      `json:"issue_comment_url"`
	IssueEventsURL   string      `json:"issue_events_url"`
	IssuesURL        string      `json:"issues_url"`
	KeysURL          string      `json:"keys_url"`
	LabelsURL        string      `json:"labels_url"`
	Language         string      `json:"language"`
	LanguagesURL     string      `json:"languages_url"`
	MasterBranch     string      `json:"master_branch"`
	MergesURL        string      `json:"merges_url"`
	MilestonesURL    string      `json:"milestones_url"`
	MirrorURL        interface{} `json:"mirror_url"`
	Name             string      `json:"name"`
	NetworkCount     float64     `json:"network_count"`
	NotificationsURL string      `json:"notifications_url"`
	OpenIssues       float64     `json:"open_issues"`
	OpenIssuesCount  float64     `json:"open_issues_count"`
	Owner            struct {
		AvatarURL         string  `json:"avatar_url"`
		EventsURL         string  `json:"events_url"`
		FollowersURL      string  `json:"followers_url"`
		FollowingURL      string  `json:"following_url"`
		GistsURL          string  `json:"gists_url"`
		GravatarID        string  `json:"gravatar_id"`
		HtmlURL           string  `json:"html_url"`
		ID                float64 `json:"id"`
		Login             string  `json:"login"`
		OrganizationsURL  string  `json:"organizations_url"`
		ReceivedEventsURL string  `json:"received_events_url"`
		ReposURL          string  `json:"repos_url"`
		SiteAdmin         bool    `json:"site_admin"`
		StarredURL        string  `json:"starred_url"`
		SubscriptionsURL  string  `json:"subscriptions_url"`
		Type              string  `json:"type"`
		URL               string  `json:"url"`
	} `json:"owner"`
	Private         bool    `json:"private"`
	PullsURL        string  `json:"pulls_url"`
	PushedAt        string  `json:"pushed_at"`
	Size            float64 `json:"size"`
	SshURL          string  `json:"ssh_url"`
	StargazersURL   string  `json:"stargazers_url"`
	StatusesURL     string  `json:"statuses_url"`
	SubscribersURL  string  `json:"subscribers_url"`
	SubscriptionURL string  `json:"subscription_url"`
	SvnURL          string  `json:"svn_url"`
	TagsURL         string  `json:"tags_url"`
	TeamsURL        string  `json:"teams_url"`
	TreesURL        string  `json:"trees_url"`
	UpdatedAt       string  `json:"updated_at"`
	URL             string  `json:"url"`
	Watchers        float64 `json:"watchers"`
	WatchersCount   float64 `json:"watchers_count"`
}

CLI Installation

$ go get github.com/ChimeraCoder/gojson/gojson

Assuming $GOPATH/bin is in your PATH, you can now invoke gojson directly.

API Installation

$ go get github.com/ChimeraCoder/gojson/gojson

Development

$ git clone https://github.com/ChimeraCoder/gojson.git
$ cd gojson
$ go test

Building CLI

$ go build -o _build/gojson ./gojson

Installing CLI

$ go install ./gojson

Formatting

$ gofmt -w -e -s -l .

Related Work

github.com/str1ngs/jflect

License

gojson is free software distributed under Version 3 of the GNU Public License.

As of the time of writing, this is the same license used for gcc (and therefore gccgo), so it is unlikely to restrict use in any way. Note that the GPL does not extend to any output generated by gojson; the GPL only applies to software which includes copies of gojson itself.

Issues
  • Changes how whole numbers are handled in YAML

    Changes how whole numbers are handled in YAML

    Summary

    This changes gojson’s behavior with YAML output so that it doesn’t try to infer whether YAML integers are floats.

    Also reformatted a docstring comment to appease the linter gods.

    Motivation

    Since YAML’s spec includes unambiguous tagging for integers and floats, we don’t need to use disambiguateFloatInt to turn whole numbers from floats to integers.

    Testing

    Added tests for this behavior as well!

    Hope this is helpful @ChimeraCoder – it unfortunately doesn’t really solve the problem of #44 :(

    opened by joshu-stripe 8
  • Removing the vendor directory

    Removing the vendor directory

    I personally think that the vendor directory should be removed, because it'll be either duplicated with the current YAML package that we currently have, or out of sync with it.

    Either case, it make little sense to keep it there, IMO.

    Please consider. Thx.

    opened by suntong 3
  • added yaml support

    added yaml support

    I made a few changes and I'm not sure if you want them or not. The biggest change is adding support for generating structs from either json or yaml. You can also specify what you want to tag the struct with (in case you want both json and yaml tags, or something else like bson or whatever). I updated the struct field naming to change names in all caps with underscores to be camel case. So TEST_FIELD_NAME becomes TestFieldName instead of TESTFIELDNAME.

    I'm not sure if the yaml support interests you or not, so feel free to ignore this if it doesn't and I'll keep my fork around for anyone who wants it.

    opened by darrennoble 3
  • Allow dashes in JSON keys

    Allow dashes in JSON keys

    Via cgtdk on #go-nuts, this error currently occurs:

    ### JSON DATA (THIS LINE NOT INCLUDED)
    {
      "query-continue": {
        "recentchanges": {
          "rcstart": "2013-03-18T20:20:30Z"
        }
      },
      "query": {
        "recentchanges": [
          {
            "type": "edit",
            "ns": 4,
            "title": "Wikipedia:Deletion review\/Log\/2013 March 18",
            "rcid": 563307455,
            "pageid": 38844023,
            "revid": 545266112,
            "old_revid": 545265931,
            "timestamp": "2013-03-18T20:20:30Z"
          },
          {
            "type": "edit",
            "ns": 0,
            "title": "Pope John IV",
            "rcid": 563307454,
            "pageid": 243546,
            "revid": 545266111,
            "old_revid": 545261452,
            "timestamp": "2013-03-18T20:20:30Z"
          }
        ]
      }
    }
    ### END JSON DATA
    
    ### RESULT OF USING GOJSON:
    [email protected]:~$ gojson -file="mwdata.json"
    panic: 3:7: expected ';', found '-'
    
    goroutine 1 [running]:
    main.main()
        /home/chris/code/go/src/github.com/ChimeraCoder/gojson/gojson.go:97 +0x126
    
    goroutine 2 [syscall]:
    created by runtime.main
        /build/buildd/golang-1.0.2/src/pkg/runtime/proc.c:221
    [email protected]:~$
    

    The easiest solution is probably to replace - with _.

    opened by ChimeraCoder 3
  • Ignore tests in vendor directory for older Go versions

    Ignore tests in vendor directory for older Go versions

    ./... matches the vendor/ directory in Go, which causes spurious test failures in the yaml package we use.

    This also drops tests for 1.5 and 1.6 on OS X, because the Travis OS X image for those is not working. 1.5 and 1.6 are still tested on Linux and Windows.

    opened by ChimeraCoder 2
  • Stringify first char of key name, should it be a int.

    Stringify first char of key name, should it be a int.

    This was reported to me as an error by someone using http://json2struct.herokuapp.com/. Specific example is parsing http status code information, e.g.:

    {
       "200": 1000,
       "3xx": 10,
        // ... etc ...
    }
    

    Expected results with this change would be:

    type Foo struct {
        Two00   int `json:"200"`
        ThreeXx int `json:"3xx"`
        // ... etc ...
    }
    
    opened by jmervine 2
  • Updating for use as an imported module in addition to cli.

    Updating for use as an imported module in addition to cli.

    Functionality is identical, however, build instructions are slightly different, see Makefile for details.

    Enables usage like that of https://github.com/jmervine/gojson-http.

    Thanks, J

    opened by jmervine 2
  • Generate better substruct names

    Generate better substruct names

    I just started using the -subStruct flag because I need to be able to append sub-structs to arrays. It works great, thanks!

    The name it gives the sub-structs could be better. I understand you'd want to avoid struct name collisions but even something like OuterStruct_InnerStruct would be better and more useful than the current _sub# method. Especially with IDE autocomplete. I can choose from 4 sub-structs but without going and seeing which is which I don't actually know. Also I'm assuming that if the order in the JSON file changed the sub indexing would get messed up and everything would break.

    I'll take a look at submitting a PR soon-ish of my idea.

    opened by bhechinger 1
  • use element's json path (without path separator) as struct name

    use element's json path (without path separator) as struct name

    As a proposal to #14, I think this will generate better struct names while avoiding the issue of different structs with the same name overriding each other.

    Let me know what you think :)

    opened by lobatt 1
  • Add -forcefloats flag

    Add -forcefloats flag

    Adds a -forcefloats flag. Numbers that are integral values are turned into type int64, except if the -forcefloat flag is passed, in which case numeric values are all interpreted as float64.

    This is functionally identical to https://github.com/ChimeraCoder/gojson/pull/22 by @e-dard.

    opened by ChimeraCoder 1
  • Parse floats as float64

    Parse floats as float64

    {
        "duration": 123.0,
        "timestamp" 1234
    }
    

    should provide

    type Trace struct {
       Duration float64
       Timestamp int64
    }
    

    instead of int, int

    opened by ChimeraCoder 1
  • Add support to time.Time

    Add support to time.Time

    Hi, this is a request to extend the struct generation to support time fields. If the string matches the ISO 8601 / RFC 3339 patterns, like:

    • 2021-07-28T12:34:56Z
    • 2021-07-28T16:34:56-04:00

    Then the type should time.Time instead of string. Thanks for the consideration.

    opened by cassioconti 0
  • Allow omiting package name when generating structs

    Allow omiting package name when generating structs

    This depends on my previous PR and is branched of it: https://github.com/ChimeraCoder/gojson/pull/78

    I'm writing an integration to a thirdpary application. I'm using this library to generate structs using example inputs from the thirdparty application. The below code generates these structs and places them in one 1 file.

    The change I'm proposing allows these to be put into 1 file, otherwise I'd have to split every struct definition into separate files, this clutters a lot.

    The folder api_definitions has example output files from my thirdparty application, like customers.json and articles.json.

    ./generate.go

    package main
    
    import (
    	"io/ioutil"
    	"os"
    	"strings"
    
    	"github.com/ChimeraCoder/gojson"
    )
    
    //go:generate go run generate.go
    
    func main() {
    	const definitionsPath = "internal/third_pary/api_definitions"
    	const typesPath = "internal/third_pary/"
    
    	files, err := ioutil.ReadDir(definitionsPath)
    	if err != nil {
    		panic(err)
    	}
    	allStructs := []byte{}
    	packageIsWritten := false
    	for _, f := range files {
    		file, err := os.Open(definitionsPath + "/" + f.Name())
    		if err != nil {
    			panic(err)
    		}
    		packageName := "third_pary"
    		if packageIsWritten {
    			packageName = ""
    		}
    		structName := strings.Split(f.Name(), ".")[0]
    		output, err := gojson.Generate(file, gojson.ParseJson, structName, packageName, []string{"json"}, false, true)
    		if err != nil {
    			panic(err)
    		}
    		packageIsWritten = true
    		allStructs = append(allStructs, output...)
    	}
    
    	goStructFile, err := os.Create(typesPath + "/" + "api_types.go")
    	if err != nil {
    		panic(err)
    	}
    	_, err = goStructFile.Write(allStructs)
    	if err != nil {
    		panic(err)
    	}
    }
    
    
    #I then run this to generate my file with all the structs contained therein.
    go generate ./...
    
    opened by jhzn 0
  • -forcefloats flag does not work

    -forcefloats flag does not work

    It does not work. Input:

    echo '{"value": 9.00}' | gojson -forcefloats
    

    and the output is:

    type Foo struct {
    	Value int64 `json:"value"`
    }
    

    The -forcefloats flag does not work.

    opened by sheng-di 1
  • New tool to generate proto(protobuf) file from json

    New tool to generate proto(protobuf) file from json

    Protobuf is a general tool for generating and management cross-language structures. A tool to generate proto from json is useful.

    Here is such a tool written by me.

    Maybe you can reimplement such a tool with golang or mention it in the readme.

    opened by wweir 0
Owner
Aditya Mukerjee
Aditya Mukerjee
Go generator to copy values from type to type and fields from struct to struct. Copier without reflection.

Copygen is a command-line code generator that generates type-to-type and field-to-field struct code without adding any reflection or dependenc

SwitchUpCB 121 May 15, 2022
Style definitions for nice terminal layouts 👄

Lip Gloss Style definitions for nice terminal layouts. Built with TUIs in mind. Lip Gloss takes an expressive, declarative approach to terminal render

Charm 4.2k May 20, 2022
Easy to use library and CLI utility to generate Go struct from CSV files.

csv2struct Easy to use library and CLI utility to generate Go struct from CSV files. As a benefit, it's fully compatible with csvutil. So, structs gen

Ivan Maliovaniy 9 Jan 21, 2022
Use the command to convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)

go2struct-tool Use the command to convert arbitrary formats to Go Struct (including json, toml, yaml, etc.) Installation Run the following command und

Afeyer 1 Dec 16, 2021
An alternative syntax to generate YAML (or JSON) from commandline

yo An alternative syntax to generate YAML (or JSON) from commandline. The ultimate commanline YAML (or JSON) generator! ... I'm kidding of course! but

Luca Sepe 10 Feb 2, 2022
CLI Program for setting League Of Legends runes automatically. Written in Golang.

SimplyRunes CLI Program for setting League Of Legends runes automatically. Written in Golang. I made this, because I was tired of other apps like Poro

Natan Gardocki 0 Feb 21, 2022
Json-match - Command line util for matching values in a JSON input

json-match Match JSON input by specifying key and value > json-match -json '{\"p

Trond Boksasp 0 Jan 12, 2022
A simple golang marshaller from commandline to a struct

flagmarshal SYNOPSIS A simple golang marshaller from commandline to a struct ParseFlags(structptr interface{}) error DESCRIPTION Very simple implement

null 0 Jan 22, 2022
ets2-dlc-repacker is a Windows / Linux / MacOS CLI util to automatically repack older DLC archives for compatibility with newer versions.

ets2-dlc-repacker is a Windows / Linux / MacOS CLI util to automatically repack older DLC archives for compatibility with newer versions.

Daniel 0 Dec 26, 2021
Branch is a small CLI tool to automatically create git branches based on tickets

________ ________ ________ ________ ________ ___ ___ |\ __ \ |\ __ \ |\ __ \ |\ ___ \ |\ ___

Maikel Veen 3 Jan 14, 2022
Tool to extract log files off of the roboRIO automatically

extractor Tool to extract log files off of the roboRIO automatically Steps This program does the following steps once ran: Read from TOML config file

Team 501 - The PowerKnights 2 May 15, 2022
Struct-based argument parsing in Go

go-arg Struct-based argument parsing for Go Declare command line arguments for your program by defining a struct. var args struct { Foo string Bar b

Alex Flint 1.4k May 17, 2022
Basic command line example using golang grpc client tools

This is a basic command line interface that demonstrates using the golang GRPC API. The API is reused between dishy and the wifi router, however both

null 32 Apr 27, 2022
google indexing api example (golang)

gindex google indexing api example (golang) 動機 search console のカバレージを見ると、除外が 170 ほどあった。 検出 - インデックス未登録が 84 件 クロール済み - インデックス未登録が 72 件 URL 検査で「インデックス登録

Seiji MORIYA 0 Nov 15, 2021
Go-utils - Multiple tools and example for golang

go-utils Description Multiple tools and example for golang database : example to

null 0 Jan 1, 2022
Rhoas CLI as an Host example

RHOAS CLI rhoas is a command-line client for managing all of your application services on Kubernetes. Guides See our Guides for installation and usage

AeroGear 6 Nov 8, 2021
A template and a functioning example for a basic JFrog CLI plugin

hello-frog About this plugin This plugin is a template and a functioning example for a basic JFrog CLI plugin. This README shows the expected structur

Bharath Upadhya 0 Dec 21, 2021
A template and a functioning example for a basic JFrog CLI plugin

hello-frog About this plugin This plugin is a template and a functioning example for a basic JFrog CLI plugin. This README shows the expected structur

Rémi BOURGAREL 0 Nov 26, 2021