A Neovim plugin for Go developers to convert JSON to Go struct definitions. Read JSON from your clipboard, enter a struct name, and get properly formatted Go code.
- Clipboard Integration: Read JSON directly from your system clipboard
- Visual Selection: Convert selected JSON text in your buffer
- Auto Paste: Automatically inserts generated struct at cursor position
- Smart Naming: Automatically converts JSON keys to PascalCase Go field names
- Nested Structs: Generates separate struct definitions for nested objects
- Acronym Handling: Properly handles common acronyms (ID, URL, API, HTTP, JSON, etc.)
Using lazy.nvim
{
"fahmiauliarahman/json2go.nvim",
ft = "go",
config = function()
require("json2go").setup()
end,
keys = {
{ "<leader>jg", "<cmd>Json2Go<cr>", desc = "JSON to Go struct (clipboard)" },
{ "<leader>js", "<cmd>Json2GoSelection<cr>", mode = "v", desc = "JSON to Go struct (selection)" },
},
}Using packer.nvim
use {
"fahmiauliarahman/json2go.nvim",
config = function()
require("json2go").setup()
end,
}- Copy JSON to your clipboard
- Position cursor where you want the struct inserted
- Run
:Json2Goor press<leader>jg - Enter the top-level struct name when prompted
- The generated Go struct is automatically inserted at cursor position
:Json2Go Response
- Select JSON text in your buffer
- Run
:Json2GoSelectionor press<leader>js - Enter the top-level struct name when prompted
- The generated Go struct is automatically inserted at cursor position
Input JSON (in clipboard):
{
"originalExternalId": "merchant-x-external-id",
"originalPartnerReferenceNo": "merchant-order-id",
"partnerRefundNo": "merchant-refund-no",
"userId": "gopay-order-id",
"refundAmount": {
"value": "10000.00",
"currency": "IDR"
},
"reason": "some-reason",
"additionalInfo": {
"paymentProviderMetadata": {}
}
}Command:
:Json2Go RefundRequest
Output:
type RefundRequest struct {
AdditionalInfo AdditionalInfo `json:"additionalInfo"`
OriginalExternalID string `json:"originalExternalId"`
OriginalPartnerReferenceNo string `json:"originalPartnerReferenceNo"`
PartnerRefundNo string `json:"partnerRefundNo"`
Reason string `json:"reason"`
RefundAmount RefundAmount `json:"refundAmount"`
UserID string `json:"userId"`
}
type AdditionalInfo struct {
PaymentProviderMetadata PaymentProviderMetadata `json:"paymentProviderMetadata"`
}
type PaymentProviderMetadata struct {
}
type RefundAmount struct {
Currency string `json:"currency"`
Value string `json:"value"`
}require("json2go").setup({
default_struct_name = "Response", -- Default struct name shown in prompt
use_clipboard = true, -- Read from clipboard by default
auto_format = true, -- Auto-format generated code
notify_on_success = true, -- Show notification when done
auto_paste = true, -- Auto paste at cursor (set false to copy to clipboard only)
show_preview = false, -- Show floating preview window (only when auto_paste = false)
})The plugin doesn't set any keybindings by default. Recommended mappings:
vim.keymap.set("n", "<leader>jg", "<cmd>Json2Go<cr>", { desc = "JSON to Go struct (clipboard)" })
vim.keymap.set("v", "<leader>js", "<cmd>Json2GoSelection<cr>", { desc = "JSON to Go struct (selection)" })| Command | Description |
|---|---|
:Json2Go [name] |
Convert JSON from clipboard and insert at cursor. Optional struct name. |
:Json2GoSelection [name] |
Convert selected JSON and insert at cursor. Optional struct name. |
- JSON Parsing: Uses Neovim's built-in
vim.fn.json_decode() - Type Inference: Automatically detects Go types from JSON values
- Nested Processing: Recursively processes nested objects into separate structs
- Name Conversion: Converts
camelCaseandsnake_casetoPascalCase - Acronym Handling: Recognizes common acronyms (ID, URL, API, etc.) and capitalizes them
| JSON Type | Go Type |
|---|---|
string |
string |
number (integer) |
int64 |
number (float) |
float64 |
boolean |
bool |
null |
interface{} |
object |
Named struct |
array |
Slice of inferred type |
| ISO 8601 date string | time.Time |
MIT
Contributions are welcome! Please feel free to submit a Pull Request.