mholt/json-to-go

Problem with array of structs

schollz opened this issue · 4 comments

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.

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

Awesome! Thanks for your help

You're welcome!

mholt commented

Just deployed it