String Character escaping

5,103 views
Skip to first unread message

Brian Ketelsen

unread,
Jan 3, 2011, 4:10:15 PM1/3/11
to golang-nuts
I'm hand building some JSON data for a project and I'm having a hard time including the quote character " in the string. I tried to do it all inline in an Sprintf call:



j.Args[1] = fmt.Sprintf("{\"encrypted_social_security_number\"=>\"%s\", \"first_name_soundex\"=>\"%s\",\"last_name_soundex\"=>\"%s\",\"address_soundex\"=>\"%s\",\"city_soundex\"=>\"%s\"}", essn ,fns,lns,as,cs)

That didn't work, so i thought maybe it was a peculiarity of Sprintf. So I split it out like this:

json = "{\"encrypted_social_security_number\"=>\"%s\", \"first_name_soundex\"=>\"%s\",\"last_name_soundex\"=>\"%s\",\"address_soundex\"=>\"%s\",\"city_soundex\"=>\"%s\"}"

j.Args[1] = fmt.Sprintf(json, essn ,fns,lns,as,cs)

Both of these create the following:

{"\encrypted_social_security_number\" etc.

How do I insert the quote without also inserting the escape slash?

I'm sure I'm missing something obvious!

Thanks,

Brian

Brian Ketelsen

unread,
Jan 3, 2011, 4:22:12 PM1/3/11
to golang-nuts

peterGo

unread,
Jan 3, 2011, 4:40:20 PM1/3/11
to golang-nuts
Brian,

You are posting duplicate messages, 12 minutes apart. Why?

I don't see your problem.

package main

import (
"fmt"
)

func main() {
// interpreted string literal
i := "{\"encrypted_social_security_number\"=>\"%s\",
\"first_name_soundex\"=>\"%s\",\"last_name_soundex\"=>\"%s\",
\"address_soundex\"=>\"%s\",\"city_soundex\"=>\"%s\"}"
si := fmt.Sprintf(i, "aaa", "bbb", "ccc", "ddd", "eee")
fmt.Println(si)
// raw string literal
r := `{"encrypted_social_security_number"=>"%s",
"first_name_soundex"=>"%s","last_name_soundex"=>"%s","address_soundex"=>"%s","city_soundex"=>"%s"}
`
sr := fmt.Sprintf(r, "aaa", "bbb", "ccc", "ddd", "eee")
fmt.Println(sr)
}

Output:

{"encrypted_social_security_number"=>"aaa",
"first_name_soundex"=>"bbb","last_name_soundex"=>"ccc","address_soundex"=>"ddd","city_soundex"=>"eee"}
{"encrypted_social_security_number"=>"aaa",
"first_name_soundex"=>"bbb","last_name_soundex"=>"ccc","address_soundex"=>"ddd","city_soundex"=>"eee"}

String literals, The Go Programming Language Specification.
http://golang.org/doc/go_spec.html#String_literals

Peter

Brian Ketelsen

unread,
Jan 3, 2011, 7:25:56 PM1/3/11
to peterGo, golang-nuts

On Jan 3, 2011, at 4:40 PM, peterGo wrote:

> Brian,
>
> You are posting duplicate messages, 12 minutes apart. Why?

Apple Mail and Gmail infrequently get into a state where the mail sends but never leaves the drafts. I've had this happen 3 or 4 times over the past several years... I guess this was another. Apologies for the duplication!

>
> I don't see your problem.

If I had a dollar for every time I had a problem nobody else could see... :)
>

This problem turned out to be a double-encoding problem. I was assigning a json string to a variable, and the client library I'm using (that I also wrote) was json encoding the string.

Fortunately, I learned about the backtick method of creating character literals from your response. Thanks! It's nice to learn from our mistakes.

Brian

Reply all
Reply to author
Forward
0 new messages