1

I am trying to learn how to create and manipulate JSON in this format on the fly using golang:

{ 
"justanarray": [ 
    "One", 
    "Two" 
], 
"nestedstring": {"name": {"first": "Dave"}}, 
"nestedarray": [ 
    {"address": {"street": "Central"}},  
    {"phone": {"cell": "(012)-345-6789"}}  
] 
} 

I can create and manipulate everything but the nested array.

Here is a play for the code below. https://play.golang.org/p/pxKX4IOE8v

package main 

import ( 
        "fmt" 
        "encoding/json" 
) 





//############ Define Structs ################ 

//Top level of json doc 
type JSONDoc struct { 
        JustArray   []string    `json:"justanarray"` 
    NestedString    NestedString    `json:"nestedstring"` 
        NestedArray []NestedArray   `json:"nestedarray"` 


} 

//nested string 
type NestedString struct { 
        Name   Name   `json:"name"` 
} 
type Name struct { 
        First string `json:"first"` 
} 

//Nested array 
type NestedArray []struct { 
        Address   Address   `json:"address,omitempty"` 
        Phone Phone `json:"phone,omitempty"` 
} 
type Address struct { 
        Street string `json:"street"` 
} 
type Phone struct { 
        Cell string `json:"cell"` 
} 






func main() { 

        res2B := &JSONDoc{} 
    fmt.Println("I can create a skeleton json doc") 
    MarshalIt(res2B) 

    fmt.Println("\nI can set value of top level key that is an array.") 
        res2B.JustArray = []string{"One"} 
    MarshalIt(res2B)    

    fmt.Println("\nI can append this top level array.") 
        res2B.JustArray = append(res2B.JustArray, "Two") 
    MarshalIt(res2B) 

    fmt.Println("\nI can set value of a nested key.") 
        res2B.NestedString.Name.First = "Dave" 
        MarshalIt(res2B) 


    fmt.Println("\nHow in the heck do I populate, and append a nested array?") 


} 

func MarshalIt(res2B *JSONDoc){ 
        res, _ := json.Marshal(res2B) 
        fmt.Println(string(res)) 
}

Thanks for any help.

1 Answer 1

1

Instead of defining NestedArray as a slice of anonymous structs, it's better to redefine it in JSONDoc as such:

type JSONDoc struct {
    JustArray    []string          `json:"justanarray"`
    NestedString NestedString      `json:"nestedstring"`
    NestedArray  []NestedArrayElem `json:"nestedarray"`
}

//Nested array
type NestedArrayElem struct {
    Address Address `json:"address,omitempty"`
    Phone   Phone   `json:"phone,omitempty"`
}

Then, you can do:

res2B := &JSONDoc{}
res2B.NestedArray = []NestedArrayElem{
    {Address: Address{Street: "foo"}},
    {Phone: Phone{Cell: "bar"}},
}
MarshalIt(res2B)

Playground: https://play.golang.org/p/_euwT-TEWp.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Ainar-G, but this appears to have the structure of "nestedarray": [ { "address": {"street": "foo"}, "phone": {"cell": ""} }, { "address": {"street": ""}, "phone": {"cell": "bar"} } ]`
Ainar-G, Thanks again, I was able to modify your answer a bit using interfaces, and with your help I believe I am close to a solution. The last piece to my puzzle is this. How do I append? play.golang.org/p/0i0t8O_KrN Thanks!
@sneeze You don't need interfaces here, pointers are enough. Also, when you append, you should use a value, not a slice. play.golang.org/p/hs4SGHSn7Q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.