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.51k stars 475 forks source link

Error while parsing valid json #71

Closed sbres closed 2 years ago

sbres commented 5 years ago

First of all, thank you for this tool it is very helpful.

I was trying to encode this value:

[{"holder1": {"type1": [["value1", "value2"], ["value1", "value2"]]}}]

And it resulted in this error:

5:15: expected ';', found '[' (and 1 more errors)

The smallest structure that still shows the error is the following.

[{"holder1": {"type1": [["value"]]}}]

mholt commented 5 years ago

Thanks for reducing down the problem! I just pasted in your structure and it works just fine for me:

type AutoGenerated []struct {
    Holder1 struct {
        Type1 [][]string `json:"type1"`
    } `json:"holder1"`
}
sbres commented 5 years ago

I just tried it again.

It does work when "Inline type definitions" is set, but not when it's not set.

mholt commented 5 years ago

Ah, you're right. @mahdi-hosseini any idea what might be causing this?

mholt commented 5 years ago

Oh, and to be clear, I suspect this might be the output of gofmt, i.e. the Go code we generate is not syntactically valid.

mike-hosseini commented 5 years ago

Ah, you're right. @mahdi-hosseini any idea what might be causing this?

Hi Matt, I will look into it this weekend!

mike-hosseini commented 5 years ago

The issue does not seem to be caused by gofmt but rather the output of jsonToGo() is invalid Go struct. There is an extraneous Type1 in the output when the function is called with flatten = true, see here: https://play.golang.org/p/tpFHAOmFQk8

So this is probably a bug. I may be able to look into it further when I have the time.

mholt commented 5 years ago

Ah, nice catch. Thanks for looking into it!

aneeskA commented 4 years ago

I encountered the same problem. Just wondering if there is a work around for this issue.

Excellent tool, by the way.

lhlyu commented 3 years ago

@sbres @aneeskA You can try this online tool

sbres commented 2 years ago

I've done a check and the website is able to convert the JSON to a go struct