How to avoid json decoder to remove the backslash characters in the json string

I have a json string like this one:

{“toDb”:“BEGIN let rs1 resultset := (\u0002); let rs2 resultset := select c2 from t1 where RLIKE(c1,‘\d+’); END;“}

When I decode this json string into a struct, it will remove the escaping characters. That’s not what I expect. So how can I avoid this behavior? Here is the example code link: Go Playground - The Go Programming Language

I don’t see anything getting removed.

Escaped characters are properly represented in the deserialized data.

If you want to represent the string \u0002 or \\d rather than the byte 0x02 or the string \d, you need to add additional escapes in the JSON. \\u0002 and \\\\d.

I have got the solution by using map[string]json.RawMessage as a struct to decode to. Thanks for the response.

Sounds as if the real fix has to be applied at the source of the JSON…

Using RawMessage feels like a workaround for broken input, not as a real fix…

1 Like