go - json marshal one property of struct array -


so have array of struct recipe contains properties , 1 of properties struct source, want convert entire array json source property of recipe struct

code: https://play.golang.org/p/e71d4xznm4

result:

[ {     "id": 1,     "title": "fine peanutbutter",     "description": "the best peanutbutter in world",     "source": {         "name": "peter",         "address": "32121 little midge"     },     "price": 49 }, {     "id": 2,     "title": "fine jelly",     "description": "the best jelly in 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 without looping through entire array , creating new array struct , doing json marshal on one

you may define custom marshaller:

func (r recipe) marshaljson() ([]byte, error) {   return json.marshal(r.source) } 

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


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -