vsc-json2go
Turn JSON into a Go type, inside VS Code.
Select some JSON (or open a whole JSON file), run a command, and get the matching
Go struct definitions. The conversion runs locally — the extension embeds the
json2go library compiled to WebAssembly, so
nothing is sent anywhere.
Commands
Open the Command Palette and type Json2Go:
| Command |
What it does |
| Json2Go: Generate go type from selection to clipboard |
Converts the selected text, copies the Go type to the clipboard. |
| Json2Go: Generate go type from opened file to clipboard |
Converts the whole active document, copies the Go type to the clipboard. |
| Json2Go: Replace selection with generated go type |
Converts the selected text and replaces the selection with the Go type. |
With more than one cursor the commands use the primary selection only; the other
selections are left alone.
Output
Input:
{"x":1,"nested":{"z":"a","t":"2020-01-01T00:00:00Z"},"arr":[{"details":[{"a":1}]}]}
Output:
type Document struct {
Arr []Arr `json:"arr"`
Nested Nested `json:"nested"`
X int `json:"x"`
}
type Arr struct {
Details []Detail `json:"details"`
}
type Nested struct {
T time.Time `json:"t"`
Z string `json:"z"`
}
type Detail struct {
A int `json:"a"`
}
Nested objects become their own named types, and array element names are
singularized (details gives you a Detail type). Turn json2go.extractAllTypes
off if you would rather have them inline.
Settings
All settings live under Json2Go in the Settings UI.
| Setting |
Type |
Default |
Description |
json2go.rootName |
string |
Document |
Name of the top-level generated type. |
json2go.extractAllTypes |
boolean |
true |
Give every nested object its own named top-level type. |
json2go.extractCommonTypes |
boolean |
true |
Extract types that are shared between branches into one type. |
json2go.stringPointersWhenKeyMissing |
boolean |
true |
Use pointer types for keys that are not present in every object. |
json2go.skipEmptyKeys |
boolean |
true |
Ignore keys with empty names. |
json2go.useMaps |
boolean |
true |
Turn objects with uniform values into a map instead of a struct. |
json2go.useMapsMinAttrs |
number |
5 |
How many attributes an object needs before it may become a map. Minimum 1. |
json2go.timeAsString |
boolean |
false |
Emit string instead of time.Time for date-like values. |
json2go.similarityThreshold |
number |
0.7 |
How alike two objects must be before they are merged into one type. 0–1. |
json2go.minSubsetSize |
integer |
2 |
Smallest set of shared fields that can be pulled out as its own type. Minimum 1. |
json2go.minSubsetOccurrences |
integer |
2 |
How often that set must appear before it is pulled out. Minimum 1. |
json2go.minAddedFields |
integer |
2 |
How many extra fields a variant needs before it becomes a separate type. Minimum 1. |
Requirements
- VS Code 1.125 or newer.
- The generated code needs Go 1.18 or newer. json2go emits
any rather than
interface{}.
Not supported yet: web
This extension does not run in vscode.dev or github.dev. It reads its
WebAssembly module from the file system, which the browser extension host does
not provide. Use the web version below instead.
Links
Building the wasm
wasm/json2go.wasm and wasm/wasm_exec.js are committed to this repo. To
rebuild them from a local checkout of the json2go library:
git clone https://github.com/m-zajac/json2go.git ../json2go
./scripts/build-wasm.sh # or: ./scripts/build-wasm.sh /path/to/json2go
The script needs TinyGo (brew tap tinygo-org/tools && brew install tinygo) and
will tell you if it is missing. It prints the resulting file sizes and the
upstream git describe, so you can record where a build came from.
Both output files must be updated together. The .wasm and its
wasm_exec.js host shim are version-locked to the toolchain that produced them,
and TinyGo's shim is not interchangeable with standard Go's. A mismatched pair
fails at WebAssembly.instantiate with a LinkError, so the extension breaks at
startup rather than producing wrong output.
Demo

Note: this recording is from an older release. The commands still work the same
way, but the generated code is formatted differently now — see
Output above for what current versions produce.
License
MIT © Maciej Zajac