I'm having a tough time moving json over the wire.
There is a jsonb
field in a Postgres db that I need to add to a struct before sending it over the wire as an http response.
If the struct's payload field is a string
, marshaling escapes the json like "{\"id\": \"3aa5fff0-ad91-41b1-84f0-d97f38e0e0f4\", \"user\": 1 }
.
If the struct's payload field is a json.RawMessage
, marshalling escapes the json as (what I imagine to be) a sequence of base64 encoded bytes.
This is the struct that I am marshaling and writing to the http response stream:
type NestJobReturn struct {
Status string `json:"status"`
Nest json.RawMessage `json:"nest"`
}
I build a ret
instance of this struct and print it out. If I use %v
it shows bytes, and %s
shows it as the proper, un-escaped json string:
log("Value of ret.Nest: %v", ret.Nest) // Value of ret.Nest: [123 34 105 ...
log("Value of ret.Nest as a string: %s", ret.Nest) // Value of ret.Nest as a string: {"id": "f053...
Marshaling and i/o is done thusly:
js, _ := json.Marshal(ret)
res.Header().Set("Content-Type", "application/json")
res.Write(js)
The client currently receives the entire message looking kinda like this:
{"status":"ok","nest":"eyJpZCI6ICJmMD..."}
... but the intended value of "nest" is the valid json from my jsonb
column in the database.
Any ideas?