Skip to content

Repository files navigation

Constructor

Provides a quick and dirty way to convert JSON data dumps to a Golang struct.

Usage

Given the following JSON data dump:

{
"users": [
{
"first_name": "Alice",
"username": "alice",
"age": "23"
},
{
"username": "bob",
"age": 42
}
]
}

You can convert it to a Golang struct like this:

package main
import (
"bytes""fmt""os""github.com/0x51-dev/constructor""github.com/0x51-dev/constructor/fmt/gofmt"
)
funcmain() {
data, _:=os.ReadFile("data.json")
n, _:=constructor.Construct(bytes.NewReader(data))
str, _:=gofmt.NodeToGo("Users", n)
fmt.Println(str) // Prints out formatted Golang struct.
}

Output:

package users
typeUsersstruct {
Users []struct {
Ageany/* json.Number, string */`json:"age"`FirstName*string`json:"first_name"`Usernamestring`json:"username"`
} `json:"users"`
}

About

Converts JSON Data Dumps to Structs

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages