Issue with initializin struct with slice

Hi

my json look like this:

{
   "update":{
      "ticketStatusChange":{
         "statusChangeReason":2000,
         "ticketStatus":3,
         "ticketSubstatus":null,
         "additionalData":[
            {
               "key":"textWI",
               "value":"Info Operacion WI"
            },
            {
               "key":"closureProductCategoryTier1",
               "value":null
            },
            {
               "key":"closureProductCategoryTier2",
               "value":null
            },
            {
               "key":"closureProductCategoryTier3",
               "value":null
            },
            {
               "key":"resolutionCategory",
               "value":null
            },
            {
               "key":"resolutionCategoryTier2",
               "value":null
            },
            {
               "key":"resolutionCategoryTier3",
               "value":null
            },
            {
               "key":"resolution",
               "value":null
            },
            {
               "key":"outageDuration",
               "value":null
            },
            {
               "key":"requiredResolutionDateTime",
               "value":null
            },
            {
               "key":"problemRealDate",
               "value":null
            },
            {
               "key":"serviceOutageRealResolvedDate",
               "value":null
            },
            {
               "key":"cause",
               "value":null
            },
            {
               "key":"serviceCause",
               "value":null
            }
         ]
      }
   }
}

my struct is defined like:

type Tk struct {
	Update struct {
		TicketStatusChange struct {
			StatusChangeReason int         `json:"statusChangeReason"`
			TicketStatus       int         `json:"ticketStatus"`
			TicketSubstatus    interface{} `json:"ticketSubstatus"`
			AdditionalData     []struct {
				Key   string `json:"key"`
				Value string `json:"value"`
			} `json:"additionalData"`
		} `json:"ticketStatusChange"`
	} `json:"update"`
}

how to initialize AdditionalData with key value and value value.

tnx
miha

how to initialize AdditionalData with key value and value value.

You would use a slice of struct literals like this:

example := TK{
	Update: struct{}{
		TicketStatusChange: struct{}{
			StatusChangeReason: 1,
			TicketStatus: 1,
			TicketSubstatus: 1,
			AdditionalData: []struct{}{
				struct{}{
					Key: "textWI",
					Value: "Info Operacion WI",		
				},
				struct{}{
					Key: "closureProductCategoryTier1",
					Value: nil,
				},
			},
		},
	},
}

https://goplay.space/#H1KxQMunonW

1 Like

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