Priyanka
Priyanka

Reputation: 364

Golang json.Unmarshal invalid character '\n' in string literal

Golang json.Unmarshal throws error for newline character. Go Playground

How to unmarshal data if string contains newline?

Upvotes: 1

Views: 11749

Answers (1)

Aditya Satyavada
Aditya Satyavada

Reputation: 1058

Simply escaping the newline character should do the trick:

var val []byte = []byte(`"{\"channel\":\"buupr\\niya\",\"name\":\"john\", \"msg\":\"doe\"}"`)

The output for the above:

{"channel":"buupr\niya","name":"john", "msg":"doe"}

Since you're attempting to pass a raw string literal here, you will need to be able to represent the JSON in string form, which requires you to escape the newline character.

Upvotes: 3

Related Questions