A CLI helper to generate ready-to-use V structs from JSON.
Find a file
0xA672 f1d6bf3054
Revise README with installation and usage updates
Updated installation instructions and usage examples for json2v tool. Improved clarity on linting and output sections.
2026-05-16 18:21:23 +08:00
json2v.py Update version to 2.1-prod and refactor type handling 2026-05-16 09:01:35 +08:00
LICENSE Initial commit 2026-05-02 07:05:32 +08:00
README.md Revise README with installation and usage updates 2026-05-16 18:21:23 +08:00

json2v

JSON → V struct generator (enhanced)

A CLI tool that reads JSON and emits readytocompile V struct definitions.
Builtin lint checks catch naming issues, reserved words, type conflicts and more, while --fix can automatically repair many common problems.

Features

  • Smart type inference list element types, null/optional types, nested structs
  • Lint diagnostics naming conventions, reserved words, duplicate fields, type conflicts
  • Autofix camelCase → snake_case, reserved word escaping, nulltype suggestions
  • Colored terminal output, verbose & dryrun modes, and strictmode error handling
  • Reads from stdin or file; writes to stdout or file

Installation

Requirements

  • Python 3.8 or higher (no external dependencies)

Create a setup.py file in the same directory as json2v.py:

from setuptools import setup

setup(
    name='json2v',
    version='2.1',
    py_modules=['json2v'],
    entry_points={
        'console_scripts': ['json2v=json2v:main'],
    },
    python_requires='>=3.8',
)

Then install with pip:

pip install .

Now you can invoke the tool globally:

json2v -i data.json -o output.v

Alternative: manual placement

Make the script executable and move it into your PATH:

chmod +x json2v.py
sudo cp json2v.py /usr/local/bin/json2v

After that, json2v works from any terminal.

Quick start

# From stdin
echo '{"name":"Alice","age":30}' | json2v

# From a file, output to another file
json2v -i data.json -o output.v

# Lint + autofix with verbose diagnostics
json2v -i data.json --lint --fix -v

Options

Flag Default Description
-i, --input FILE stdin Input JSON file
-o, --output FILE stdout Output V file
--root NAME Root Root struct name
--lint off Enable lint diagnostics
--fix off Autofix lint issues (implies --lint)
--dry-run off Only lint, no code output (implies --lint)
-v, --verbose off Show detailed diagnostics and suggestions
--no-color off Disable colored terminal output
--strict off Treat warnings as errors; exit code 1 on any warning
--version Show version

Lint diagnostic codes

Code Severity Description
V001 Warning Empty list cannot infer element type
V002 Warning List contains mixed element types
V003 Warning Field value is null cannot determine concrete type
V004 Error Value type cannot be mapped to a V type
V010 Error/Warning Field name is a V reserved word
V011 Error/Warning Field name conflicts with a builtin V type
V012 Warning Field name is not snake_case
V013 Error/Warning Field name starts with a digit
V014 Error Name collision after autofix
V020 Error JSON root is not an object

Example

Input (data.json)

{
  "userName": "Alice",
  "userAge": 30,
  "isActive": true,
  "metadata": {
    "lastLogin": "2026-05-01",
    "score": 42.5
  },
  "tags": ["admin", "moderator"],
  "profile": null,
  "items": [
    {"id": 1, "value": "one"},
    {"id": 2, "value": "two"}
  ]
}

Command

json2v -i data.json --lint --fix -v

Generated V code (stdout)

import json

pub struct Root {
    [json: userName]
    mut user_name string
    [json: userAge]
    mut user_age int
    [json: isActive]
    mut is_active bool
    [json: metadata]
    mut metadata struct {
        [json: lastLogin]
        mut last_login string
        [json: score]
        mut score f64
    }
    [json: tags]
    mut tags []string
    [json: profile]
    mut profile ?any
    [json: items]
    mut items []Item
}

pub struct Item {
    [json: id]
    mut id int
    [json: value]
    mut value string
}

Lint report (stderr, with -v)

══════════════════════════════════════════════════════
  Lint Report
══════════════════════════════════════════════════════

  ⚠ Warnings (2)
    [V012] 'Root.userName' invalid naming
      Path: Root.userName
      Fix: -> user_name
    [V003] Null at 'Root.profile'
      Path: Root.profile
      Fix: Replace ?any

  Total: 2 warning(s)
══════════════════════════════════════════════════════

License

MIT see LICENSE.