Skip to content

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.

Notifications You must be signed in to change notification settings

fahmiauliarahman/json2go.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json2go.nvim

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.

Features

  • 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.)

Installation

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)" },
  },
}
use {
  "fahmiauliarahman/json2go.nvim",
  config = function()
    require("json2go").setup()
  end,
}

Usage

From Clipboard

  1. Copy JSON to your clipboard
  2. Position cursor where you want the struct inserted
  3. Run :Json2Go or press <leader>jg
  4. Enter the top-level struct name when prompted
  5. The generated Go struct is automatically inserted at cursor position
:Json2Go Response

From Visual Selection

  1. Select JSON text in your buffer
  2. Run :Json2GoSelection or press <leader>js
  3. Enter the top-level struct name when prompted
  4. The generated Go struct is automatically inserted at cursor position

Example

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"`
}

Configuration

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)
})

Keybindings

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)" })

Commands

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.

How It Works

  1. JSON Parsing: Uses Neovim's built-in vim.fn.json_decode()
  2. Type Inference: Automatically detects Go types from JSON values
  3. Nested Processing: Recursively processes nested objects into separate structs
  4. Name Conversion: Converts camelCase and snake_case to PascalCase
  5. Acronym Handling: Recognizes common acronyms (ID, URL, API, etc.) and capitalizes them

Supported Types

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

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published