How to use json.marshal encode struct to Json array format contains difference object types

"FeedTypes": [{
"Feeds": [{
	"FeedType": 1,
	"ProductId": 1,
	"ShopId": 3,
	"ShopName": "French Connection",
	"ShopImage": "/French+Connection/French_Connection-Logo-Actual.png",
	"ProductName": "Be A Champion Sports Bra",
	"ProductThumbnails": [
	"CreateTime": 1453917000,
	"Like": 1000,
	"Description": "Nisolo is an impact focused lifestyle brand that ethically sources products in a manner that positively impacts consumers and producers. For producers, Nisolo offers skills training and safe working conditions, facilitates international market access, and ensures beyond fair trade wages. For consumers, Nisolo’s handmade, classic essentials offer superior design, effortless style, and the ability for consumers to improve livelihoods through their purchases.",
	"Caption": "Comfy way to train and exercise",
	"Price": 1700,
	"Inventory": 20,
	"IsExclusive": true,
	"Promotion": {
		"PromotionPercentage": 30,
		"PromotionPrice": 500
}, {
	"FeedType": 2,
	"ShopId": 1,
	"ShopName": "Leibal",
	"ShopImage": "/Buck+Mason/images.png",
	"ShopDescription": "Home&Living. Art Media",
	"ShopFollowingStatus": true,
	"Products": [{
		"Id": 1,
		"Name": "The Abyss Table",
		"Image": "/Native+Union/Product+2/source_photo-225.jpg",
		"Price": 40490,
		"Inventory": 20,
		"IsExclusive": true,
		"Promotion": {
			"PromotionPercentage": 30,
			"PromotionPrice": 500
	}, {
		"Id": 2,
		"Name": "Chic Paradise",
		"Image": "/French+Connection/Product+2/dsaa.jpg",
		"Price": 12490,
		"Inventory": 100,
		"IsExclusive": true
	}, {
		"Id": 3,
		"Name": "Black Butterfly Cross",
		"Image": "/Native+Union/Product+3/Product+3-Main.jpg",
		"Price": 2400,
		"Inventory": 5,
		"IsExclusive": false


I have difference feed type as JSON Format to response to client app like this example.
So i would like to declare “struct” and use it in json.Marshal function. what’s the best practice to do this ?

Thanks for advice

Maybe you can cheat by embedding;

That’s for unmarshalling, but same should work for marshalling with judicious use of json:",omitempty".

Thanks so much Calmh for the advice :smile: . I tried to add different struct to []interface it also works great.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.