1

I often have to work with nested JSON in Go and I'm wondering what's the most correct or idiomatic way to create the struct it's parsed into.

Single struct:

type myStruct struct {
    Fields struct {
        Struct0 struct {
            Field0 string `json:"value"`
        } `json:"tag0"`
        Struct1 struct {
            Field0 int `json:"value"`
        } `json:"tag1"`
        Struct2 struct {
            Field0 int `json:"value"`
        } `json:"tag2"`
    } `json:"fields"`
}

Multiple structs:

type myStruct struct {
    Fields struct {
        Struct0 `json:"tag0"`
        Struct1 `json:"tag1"`
        Struct2 `json:"tag2"`
    } `json:"fields"`
}

type Struct0 struct {
    Field0 string `json:"value"`
}

type Struct1 struct {
    Field0 int `json:"value"`
}

type Struct2 struct {
    Field0 int `json:"value"`
}

Is one way better than the other?

John Smith
  • 381
  • 1
  • 5
  • 12

4 Answers4

1

There is no "better" way, both are valid and might be useful depending on your use case.

Single structs

  • Allow you to see the whole definition in one single place, which might be more clear at times
  • Result in more concise definition code

Multiple structs

  • Allow you to separate "structure" (the base struct) from objects (the sub-structs)
  • Might make it simpler to change sub-structs
  • Allow you to re-use some of the sub-structs if needed

I think that last point (multiple structs allowing you to re-use the sub-structs) is the key difference here, so if your sub-structs might be useful separately you would be better served by multiple structs.

eugenioy
  • 11,825
  • 28
  • 35
0

I prefer single structs, just seems cleaner to me, but someone with more network experience might be able to chime in as what is the industry norm.

Bazgrim
  • 169
  • 1
  • 6
0

This can be really subjective, but it depends on how you're going to use the data and if/how your JSON data may change over time.

If you have a strong belief that your JSON structure will not change, the single struct should be fine.

If you believe that your JSON structure may change over time, especially if you're dealing with large JSON objects that might come from an external source, multiple structs can be more maintainable.

I personally don't like the single struct for a general use case, I almost always find that it gets harder to maintain over time. When I have multiple versions of the JSON struct with varying forms of data, I find it much easier to use anonymous fields.

Dan 0
  • 914
  • 7
  • 15
0

If you want to test your JSON then use multiple structs.

Otherwise you cannot initialize the struct outside of a function (concisely) for use in table tests.

Initialize a nested struct in Golang is a question which deals with that...

In any case, there are pluses and minuses to both methods but ultimately it depends on your use case:

Multiple Structs

  • Easier to test
  • Modular design can be reused

Nested Anonymous Stucts

  • Concise
  • Everything in one place :)

Effectively, not much of a difference aside from testing. I'd go with multiple explicit structs for that very reason.

Community
  • 1
  • 1
Nevermore
  • 7,141
  • 5
  • 42
  • 64