Need help with take input as string later map it to any json

in my struct I have taken defaultResponse as string, but later I want to map it as different type of json struct based on few condition. But I am unable to take defaultResponse as string and pass some string

Hi @Saral_Sharma,

The playground code does not compile. After removing two backticks inside the body string, the code compiles and produces this output:

Hello GO
erro101
invalid character 't' after object key:value pair
2009/11/10 23:00:00 Operation: 
erro102
invalid json body provided for the request

Is this the error message that you opened this thread for?

type AddEndpointRequest struct {
Method string json:"method"
Path string json:"path"
ContentType int json:"contentType"
DefaultWaitTimeInMillis int64 json:"defaultWaitTimeInMillis"
DefaultStatusCode int json:"defaultStatusCode"
DefaultResponse string json:"defaultResponse"
Filters []EndpointFilterRequest json:"filters"
}

I want to pass “defaultResponse” attribute as JSON string

if I pass this : “defaultResponse”:{“title”:“Buy cheese and bread for breakfast.”}
Getting error : json: cannot unmarshal object into Go struct field AddEndpointRequest.defaultResponse of type string
So I want to pass “defaultResponse” attribute as string and later map it to JSON Object , but it’s failing

body := `{"method":"GET","path":"/getMMEPicks/","contentType":1,"defaultWaitTimeInMillis":100,"defaultStatusCode":200,"defaultResponse":{"title":"Buy cheese and bread for breakfast."},"filters":[{"headerParams":{"contentType":"application/json","userid":"s0s024v"},"queryParams":{"pagesize":"2000","status":"all"},"pathParams":{"countryCode":"US","store":"2686"},"body":"hello","waitTimeInMillis":100,"statusCode":200,"response":"200"}]}`

I further tried to simplify the query
Here I want to take DefaultResponse as String but later validate if it’s a valid json before mapping to any json struct.

Complete code with validation can be found here : Go Playground - The Go Programming Language

type AddEndpointRequest struct {
    Method            string `json:"method"`
    ContentType       int    `json:"contentType"`
    DefaultStatusCode int    `json:"defaultStatusCode"`
    DefaultResponse   string `json:"defaultResponse"`
}

I tried different option but none of them are working

  1. if I pass this : “defaultResponse”:{“title”:“Buy cheese and bread for breakfast.”} Getting error : json: cannot unmarshal object into Go struct field AddEndpointRequest.defaultResponse of type string

body := {"method":"GET","contentType":1,"defaultWaitTimeInMillis":100,"defaultStatusCode":200,"defaultResponse":"[{"p":"k"}]"}

Error : invalid character ‘p’ after object key:value pair

  1. body := {"method":"GET","contentType":1,"defaultWaitTimeInMillis":100,"defaultStatusCode":200,"defaultResponse":"{“p”:“k”}"} ./prog.go:21:117: syntax error: unexpected { at end of statement
  1. body := {"method":"GET","contentType":1,"defaultWaitTimeInMillis":100,"defaultStatusCode":200,"defaultResponse":"{/"p/":/"k/"}"} Error : ./prog.go:21:130: syntax error: unexpected literal "} at end of statement

And many more

Thanks for explaining, so you want to unmarshal JSON data into a struct but keep part of the data as a string.

Do you have control over the input data? If you wrap the value of the defaultResponse object into a string, it will stay a string and not get unmarshalled further.

"defaultResponse”: "{\“title\”:\“Buy cheese and bread for breakfast.\”}"

thanks it worked

1 Like

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