json-to-struct

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License: GPL-3.0 Imports: 23 Imported by: 0

README

json-to-struct

SLSA 3

json-to-struct attempts to generate go struct definitions from json documents

Online Version Here

Example

$ curl -s https://api.github.com/users/tmc | json-to-struct -name=User
package main

type User struct {
	AvatarURL         string      `json:"avatar_url"`
	Bio               interface{} `json:"bio"`
	Blog              string      `json:"blog"`
	Company           string      `json:"company"`
	CreatedAt         string      `json:"created_at"`
	Email             string      `json:"email"`
	EventsURL         string      `json:"events_url"`
	Followers         float64     `json:"followers"`
	FollowersURL      string      `json:"followers_url"`
	Following         float64     `json:"following"`
	FollowingURL      string      `json:"following_url"`
	GistsURL          string      `json:"gists_url"`
	GravatarID        string      `json:"gravatar_id"`
	Hireable          bool        `json:"hireable"`
	HtmlURL           string      `json:"html_url"`
	ID                float64     `json:"id"`
	Location          string      `json:"location"`
	Login             string      `json:"login"`
	Name              string      `json:"name"`
	OrganizationsURL  string      `json:"organizations_url"`
	PublicGists       float64     `json:"public_gists"`
	PublicRepos       float64     `json:"public_repos"`
	ReceivedEventsURL string      `json:"received_events_url"`
	ReposURL          string      `json:"repos_url"`
	StarredURL        string      `json:"starred_url"`
	SubscriptionsURL  string      `json:"subscriptions_url"`
	Type              string      `json:"type"`
	UpdatedAt         string      `json:"updated_at"`
	URL               string      `json:"url"`
}

Installation

$ go install github.com/tmc/json-to-struct@latest

Features

  • Struct Extraction: Automatically extract repeated nested structures with -extract-structs
  • Streaming Mode: Progressive output for large datasets with -stream
  • Roundtrip Validation: Verify generated structs with -roundtrip
  • Field Statistics: Add occurrence metadata with -stat-comments
  • Custom Field Ordering: Control field order with -field-order (alphabetical, encounter, common-first, rare-first)
  • Template Support: Custom output formatting via -template
  • NDJSON Support: Process newline-delimited JSON

Usage

# Basic usage
$ curl -s https://api.github.com/users/tmc | json-to-struct -name=User

# Extract repeated structs to reduce duplication
$ cat data.json | json-to-struct -name=Data -extract-structs

# Generate with field statistics comments
$ cat data.json | json-to-struct -name=Data -stat-comments

# Validate generated structs with roundtrip testing
$ cat data.json | json-to-struct -name=Data -roundtrip

# Process large datasets with streaming output
$ cat large.json | json-to-struct -name=Data -stream

# Custom field ordering by occurrence frequency
$ cat data.json | json-to-struct -name=Data -field-order=common-first

github.com/ChimeraCoder/gojson github.com/str1ngs/jflect

License

json-to-struct 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 json-to-struct; the GPL only applies to software which includes copies of json-to-struct itself.

Documentation

Overview

Command json-to-struct generates Go struct definitions from JSON documents.

json-to-struct

Command json-to-struct generates Go struct definitions from JSON documents.

json-to-struct reads from stdin and prints to stdout, making it easy to integrate into shell pipelines and build workflows.

```sh $ json-to-struct -h Usage of json-to-struct:

-cpuprofile string
	write CPU profile to file
-extract-structs
	if true, extracts repeated nested structs to reduce duplication
-field-order string
	field ordering: alphabetical, encounter, common-first, or rare-first (default "alphabetical")
-name string
	the name of the struct (default "Foo")
-omitempty
	if true, emits struct field tags with 'omitempty' (default true)
-pkg string
	the name of the package for the generated code (default "main")
-pprof string
	pprof server address (e.g., :6060)
-roundtrip
	if true, generates and runs a round-trip validation test
-stat-comments
	if true, adds field statistics as comments
-stream
	if true, shows progressive output with terminal clearing
-template string
	path to txtar template file
-update-interval int
	milliseconds between stream mode updates (default 500)

```

It effectively exposes JSON-to-Go struct conversion for use in shells.

Example

Given a JSON API response:

$ curl -s https://api.github.com/users/tmc | json-to-struct -name=User

Produces

package main

type User struct {
	AvatarURL         string  `json:"avatar_url,omitempty"`
	Bio               string  `json:"bio,omitempty"`
	Blog              string  `json:"blog,omitempty"`
	Company           string  `json:"company,omitempty"`
	CreatedAt         string  `json:"created_at,omitempty"`
	Email             any     `json:"email,omitempty"`
	EventsURL         string  `json:"events_url,omitempty"`
	Followers         float64 `json:"followers,omitempty"`
	FollowersURL      string  `json:"followers_url,omitempty"`
	Following         float64 `json:"following,omitempty"`
	FollowingURL      string  `json:"following_url,omitempty"`
	GistsURL          string  `json:"gists_url,omitempty"`
	GravatarID        string  `json:"gravatar_id,omitempty"`
	Hireable          bool    `json:"hireable,omitempty"`
	HtmlURL           string  `json:"html_url,omitempty"`
	ID                float64 `json:"id,omitempty"`
	Location          string  `json:"location,omitempty"`
	Login             string  `json:"login,omitempty"`
	Name              string  `json:"name,omitempty"`
	NodeID            string  `json:"node_id,omitempty"`
	OrganizationsURL  string  `json:"organizations_url,omitempty"`
	PublicGists       float64 `json:"public_gists,omitempty"`
	PublicRepos       float64 `json:"public_repos,omitempty"`
	ReceivedEventsURL string  `json:"received_events_url,omitempty"`
	ReposURL          string  `json:"repos_url,omitempty"`
	SiteAdmin         bool    `json:"site_admin,omitempty"`
	StarredURL        string  `json:"starred_url,omitempty"`
	SubscriptionsURL  string  `json:"subscriptions_url,omitempty"`
	Type              string  `json:"type,omitempty"`
	UpdatedAt         string  `json:"updated_at,omitempty"`
	URL               string  `json:"url,omitempty"`
}

Jump to

Keyboard shortcuts

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