百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

json2struct

> 开发工具
开源

用于将 JSON 转换为结构类型定义的 CLI 工具

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于将 JSON 转换为结构类型定义的 CLI 工具

json2struct

CLI tool to convert JSON to struct type definitions

At some point when dealing with JSONs in Go, you will have to write struct types to json.Unmarshal your JSONs into. Doing this by hand is not only repetitive and time consuming, but also error prone. json2struct saves you this work by automatically parsing the JSON and generating you the matching struct type definitions ready to be used.

Unlike other tools, json2struct tries to avoid generating interface{} and map[string]interface{} as much as possible. Nonetheless it's very fast .

Installation

Homebrew

bash
brew tap marhaupe/json2struct https://github.com/marhaupe/json2struct

brew install marhaupe/json2struct/json2struct

Manually

Grab the latest release binaries.

Usage

json2struct [options]

Calling json2struct without flags opens a text editor. Simply input your JSON and save and exit.

Options

You probably don't want to manually write that 1MB JSON you have to generate a struct for by hand. I mean, if you really want to, I'm not here to judge, but that's not the point. These options will make your life easier. If you miss some, feel free to open an issue.

Generating a struct from a string

-s, --string string: JSON string

This is basically your bread and butter thanks to pipes. Usage:

bash
 json2struct -s "$(curl "https://reqres.in/api/users?page=2")"

Generating a struct from an existing file

-f, --file string: path to JSON file

This is useful if you have a JSON file stored in your filesystem and are too lazy to use pipes. Usage:

bash
json2struct -f input.json

Generating a struct from the clipboard to the clipboard

-c, --clipboard: read from and write types to clipboard

Reads JSON from clipboard, generates types and writes those types to the clipboard.

bash
json2struct -c

Other options

-b, --benchmark: measure execution time

-h, --help: help for json2struct

--version: version for json2struct

Benchmarks

Command Mean [ms] Min [ms] Max [ms] Relative
json2struct -f big_reddit_response 8.1 ± 0.6 7.3 11.3 1.00
gojson -input big_reddit_response 9.0 ± 0.5 8.1 10.8 1.12 ± 0.10
cat big_reddit_response | gojsonstruct 71.7 ± 1.3 69.9 76.9 8.87 ± 0.64
quicktype -l go -o output.go big_reddit_response 329.5 ± 2.2 326.7 333.5 40.75 ± 2.86

Specifics

A lot of the lexing/parsing in this project is inspired by the text/template package. Rob Pike also gave a talk about how and why they wrote the package. The link can be found here, and the slides to it here

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gocligeneratorgogolang

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具