5

I need to decode a JSON string which has "\n" in it:

[
    {"Name":"Neo", "Message":"Hi\n:Hello everyone"},
    {"Name":"Sam","Messsage":"Hello\nEveery\nOne"}
]

I use the Golang code below:

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Messages  []string     `json:"Name,omitempty"`
}

func main() {
s := "[{\"Name\":\"Neo\", \"Message\":\"Hi\n:Hello everyone\"},   {\"Name\":\"Sam\",\"Messsage\":\"Hello\nEveery\nOne\"}]"
var pro Person
err := json.Unmarshal([]byte(s), &pro)
if err == nil {
fmt.Printf("%+v\n", pro)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", err)
}
}

But I get the error:

ERROR invalid character '\n' in string literal
6
  • Sorry. my bad. Updated the question Commented Aug 16, 2016 at 14:31
  • Can you reproduce this on the same site Mellow linked to? Commented Aug 16, 2016 at 14:32
  • stil doesn't give an error. Commented Aug 16, 2016 at 14:33
  • Pls check my updated question @tadman Commented Aug 16, 2016 at 14:53
  • 4
    The problem you have is how you define the example string. In your example you have to double escape it like so: "[{\"Message\": \"Hi\\n:Hello\"}]. But you should really use this syntax `[{"Message": "Hi\n:Hello"}]` for readability, as suggested in the play link in earlier comment. Commented Aug 16, 2016 at 14:58

2 Answers 2

14

There are a few of issues here. The first is that newline is not allowed in a JSON string. Use the two bytes \n to specify a newline, not an actual newline. If you use an interpreted string literal, then the \ must be quoted with a \. Example:

"Hello\\nWorld"

No quoting is required in a raw string literal:

`Hello\nWorld`

The next issue is that JSON value is an array of object values. To handle the array, unmarshal to a slice:

var pro []Person
err := json.Unmarshal([]byte(s), &pro)

To handle the objects, define Person as a struct:

type Person struct {
  Name    string
  Message string
}

working example on the playground.

Sign up to request clarification or add additional context in comments.

Comments

2

Use backtick, like this working sample code:

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Name    string `json:"Name"`
    Message string `json:"Message"`
}

func main() {
    s := `
[
    {"Name":"Neo", "Message":"Hi\n:Hello everyone"},
    {"Name":"Sam", "Message":"Hello\nEvery\nOne"}
]   
    `
    var pro []Person
    err := json.Unmarshal([]byte(s), &pro)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("%q\n", pro)
    fmt.Println()
    fmt.Println(pro)

}

output:

[{"Neo" "Hi\n:Hello everyone"} {"Sam" "Hello\nEvery\nOne"}]

[{Neo Hi
:Hello everyone} {Sam Hello
Every
One}]

1 Comment

@hardy I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.