1

Lets look at the following code snippet:

type Input struct {
    Value1   string
    Value2   string
    Value3   string
    Value4   string
    Nest         
}

type Nest struct {
    ID  string
}
input := &Input{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&input); err != nil {
    fmt.Printf("something went wrong %v", err)
}
fmt.Printf("Json Input = %+v\n", input)

I'm sending the following via cURL:

curl -k -vvv  -X POST -d '{"value1":"test", "value2":"Somevalue", "value3":"othervalue", "Nest":{"ID": "12345"}}' http://localhost:8000/endpoint

.. and get the following output:

{Value1:test Value2:Somevalue Value3:othervalue Value4: Nest:{ID:}}

Problem:

I'm not getting a good decoding of the nested struct for some reason. Moreover, I'm not really sure if it's my code or the way I'm calling it.

1 Answer 1

4

Nest is embedded in Input.

The JSON {"value1":"test", "value2":"Somevalue", "value3":"othervalue", "ID": "12345"} will be correctly marshalled into your Input.

If you want to use the JSON body from your Question then you will have to change Input to the following

type Input struct {
    Value1   string
    Value2   string
    Value3   string
    Value4   string
    Nest     Nest    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Strange, I thought I had tried that already. It did work this time though. Thanks!

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.