Skip to content

The JSON to Struct Generator is a simple tool written in Go that generates Go struct definitions based on JSON input. It's useful for quickly creating Go struct types that match the structure of JSON data.

License

Notifications You must be signed in to change notification settings

abhi16180/json2struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON to Struct Generator

This tool converts JSON data into Go struct definitions.

Overview

The JSON to Struct Generator is a simple tool written in Go that generates Go struct definitions based on JSON input. It's useful for quickly creating Go struct types that match the structure of JSON data.

Features

  • Converts JSON data to Go struct definitions
  • Handles nested structures, arrays, and complex JSON data
  • Copies generated struct definitions to the clipboard

Usage

Clone the repository and run the tool with the following command:

go run main.go <json-file path>

sample.json

{
  "name": "John",
  "age": 30,
  "cars": {
    "car1": "Ford",
    "car2": "BMW",
    "car3": "Fiat",
    "custom": {
      "name": "custom",
      "age": 30,
      "is_student": true,
      "grades": [
        1,
        2,
        3
      ],
      "address": {
        "city": "New York",
        "zip_code": "10001",
        "coordinates": {
          "latitude": 40.7128,
          "longitude": 74.0060
        }
      },
      "friends": [
        {
          "city": "New York",
          "zip_code": "10001"
        }
      ]
    }
  }
}

sample output

type Base struct {
    Name string  `json:"name"`
    Age  float64 `json:"age"`
    Cars Cars    `json:"cars"`
}
type Cars struct {
    Car1   string `json:"car1"`
    Car2   string `json:"car2"`
    Car3   string `json:"car3"`
    Custom Custom `json:"custom"`
}
type Custom struct {
    Age       float64   `json:"age"`
    IsStudent bool      `json:"is_student"`
    Grades    []float64 `json:"grades"`
    Address   Address   `json:"address"`
    Friends   []Friends `json:"friends"`
    Name      string    `json:"name"`
}
type Address struct {
    City        string      `json:"city"`
    ZipCode     string      `json:"zip_code"`
    Coordinates Coordinates `json:"coordinates"`
}
type Friends struct {
    ZipCode string `json:"zip_code"`
    City    string `json:"city"`
}
type Coordinates struct {
    Latitude  float64 `json:"latitude"`
    Longitude float64 `json:"longitude"`
}

About

The JSON to Struct Generator is a simple tool written in Go that generates Go struct definitions based on JSON input. It's useful for quickly creating Go struct types that match the structure of JSON data.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages