Automatically generate Go (golang) struct definitions from example JSON

Related tags

go cli golang go-generate gojson
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
  • Add float option

    Add float option

    Addresses #21.

    Add support for enforcing all JSON numbers are converted to float64 typed values.

    opened by e-dard 7
  • Pull from Ladicle/gojson

    Pull from Ladicle/gojson

    The recent work done by Ladicle/gojson seems to be interesting, and would be helpful for people in general.

    CAUTION this PR also close #48!

    opened by suntong 6
  • 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
  • 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
  • fix fmtFieldName support golint

    fix fmtFieldName support golint

    fixed in golint based on https://github.com/golang/lint/blob/master/lint.go#L617-L720 reference.

    this support fooId -> fooID.

    opened by kyokomi 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
  • 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
  • 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
  • Support time.Time types

    Support time.Time types

    This PR adds support to generate structs with time.Time instead of string when the format matches ISO 8601 / RFC 3339.

    opened by cassioconti 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
  • Migrated dependency management to go modules

    Migrated dependency management to go modules

    Removed dep as dependency manager and changed to go modules. Removed vendor folder as well.

    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 378978764 1
  • fix interface slice scenario

    fix interface slice scenario

    When the JSON is of format [interface,interface,interface] gojson has trouble printing out the substructs.

    opened by pshah28 0
  • 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
  • 
Fix function comments based on best practices from Effective Go

    Fix function comments based on best practices from Effective Go

    Every exported function in a program should have a doc comment. The first sentence should be a summary that starts with the name being declared. From effective go.

    PR generated by CodeLingo. Install here to drive Continuous Higher Standards.

    opened by CodeLingoBot 1
  • Use json path as struct name

    Use json path as struct name

    Re-opening pr #71 with a new branch.

    quoted description from original PR:

    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 0
Owner
Aditya Mukerjee
Aditya Mukerjee
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Masaaki Goshima 1.2k Sep 24, 2021
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Josh Baker 9k Sep 23, 2021
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Aliaksandr Valialkin 1.3k Sep 22, 2021
Fastest JSON interperter for golang

Welcome To JIN "Your wish is my command" Fast and Easy Way to Deal With JSON Jin is a comprehensive JSON manipulation tool bundle. All functions teste

eco 36 Sep 7, 2021
Fast JSON serializer for golang.

easyjson Package easyjson provides a fast and easy way to marshal/unmarshal Go structs to/from JSON without the use of reflection. In performance test

Free and open source software developed at Mail.Ru 3.3k Sep 15, 2021
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

William Poussier 128 Sep 24, 2021
A fast json parser for go

rjson rjson is a json parser that relies on Ragel-generated state machines for most parsing. rjson's api is minimal and focussed on efficient parsing.

WillAbides 44 Aug 21, 2021
Make JSON greppable!

gron transforms JSON into discrete assignments to make it easier to grep for what you want and see the absolute 'path' to it. It eases the exploration of APIs that return large blobs of JSON but have terrible documentation.

Tom Hudson 10k Sep 21, 2021
An efficient JSON decoder

pkg/json An alternative JSON decoder for Go. Features pkg/json aims to be a drop in replacement for encoding/json. It features: json.Scanner which, wh

null 281 Sep 21, 2021
One of the fastest alternative JSON parser for Go that does not require schema

Alternative JSON parser for Go (10x times faster standard library) It does not require you to know the structure of the payload (eg. create structs),

Leonid Bugaev 4.2k Sep 26, 2021
vjson is a Go package that helps to validate JSON objects in a declarative way.

vjson vjson is a Go package that helps to validate JSON objects in a declarative way. Getting Started Installing For installing vjson, use command bel

Milad Ebrahimi 18 Sep 21, 2021
Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

William Poussier 111 Aug 30, 2021
Create go type representation from json

json2go Package json2go provides utilities for creating go type representation from json inputs. Json2go can be used in various ways: CLI tool Web pag

Maciek Zając 89 Sep 22, 2021
Golang port of simdjson: parsing gigabytes of JSON per second

This is a Golang port of simdjson, a high performance JSON parser developed by Daniel Lemire and Geoff Langdale. It makes extensive use of SIMD instructions to achieve parsing performance of gigabytes of JSON per second.

High Performance, Kubernetes Native Object Storage 1.1k Sep 16, 2021
Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Stepan Pyzhov 73 Sep 17, 2021
A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Guillaume de Sagazan 84 Aug 20, 2021
A high-performance 100% compatible drop-in replacement of "encoding/json"

A high-performance 100% compatible drop-in replacement of "encoding/json" You can also use thrift like JSON using thrift-iterator Benchmark Source cod

Jsoniter 9.8k Sep 16, 2021
Arbitrary transformations of JSON in Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Qntfy 205 Sep 17, 2021
Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Qntfy 205 Sep 17, 2021