json封送结构数组的一个属性

So I have an array of struct Recipe it contains some properties and one of the properties is the struct Source, I want to convert the entire array to json but only the Source property of the Recipe struct

Code: https://play.golang.org/p/E71d4xzNM4

Result:

[
{
    "Id": 1,
    "Title": "Fine Peanutbutter",
    "Description": "The best peanutbutter in the world",
    "Source": {
        "Name": "Peter",
        "Address": "32121 Little Midge"
    },
    "Price": 49
},
{
    "Id": 2,
    "Title": "Fine Jelly",
    "Description": "The best Jelly in the world",
    "Source": {
        "Name": "Peter",
        "Address": "32121 Little Midge"
    },
    "Price": 39
}
]

Wanted Result:

[
{
    "Name": "Peter",
    "Address": "32121 Little Midge"     
},
{
    "Name": "Peter",
    "Address": "32121 Little Midge"
}
]

How do I get this without looping through the entire array and creating a new array struct and doing a json marshal on that one

You may define custom marshaller:

func (r Recipe) MarshalJSON() ([]byte, error) {
  return json.Marshal(r.Source)
}

https://play.golang.org/p/xLUAlMllGR

Add this to your source:

func (s Recipe) MarshalJSON() ([]byte, error) {
    return json.Marshal(s.Source)
}