mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)
https://mholt.github.io/json-to-go/
MIT License
4.5k stars 474 forks source link

Problem with array of structs #54

Closed schollz closed 5 years ago

schollz commented 5 years ago

I love this tool and love the recent upgrade to split out the different struct types!

I ran into a possible bug with arrays of structs, though. I want to fix it but I'm not sure how and could use some pointers.

I tried this minimal JSON where array contains an array of a struct {"word":"X"}:

 {
    "array": [{
        "word": "hello"
    }, {
        "word": "world"
    }]
 }

which will give me the following Inline definition:

type AutoGenerated struct {
    Array []struct {
        Word `json:"word"`
    } `json:"array"`
}
  1. There is a problem that this is not a valid inline since the Word doesn't have a type (it should be string).
  2. When I uncheck inline it will just give a different error: 2:11: expected type, found 'STRING'

I haven't seen this problem anywhere else, except when I have the array of structs.

mike-hosseini commented 5 years ago

The latest commit has resolved this, you have to wait till it gets deployed at https://mholt.github.io/json-to-go/

These are my outputs, with and without the inlining:

type AutoGenerated struct {
        Array []struct {
                Word string `json:"word"`
        } `json:"array"`
}
type AutoGenerated struct {
        Array []Array `json:"array"`
}
type Array struct {
        Word string `json:"word"`
}
schollz commented 5 years ago

Awesome! Thanks for your help

mike-hosseini commented 5 years ago

You're welcome!

mholt commented 5 years ago

Just deployed it