Reputation: 532
I'm trying to decode a string containing an escaped double quote, e.g. "I said, \"Hello\""
Now to do that in Elm, I may use Json.Decode as Decode:
Decode.decodeString Decode.string "\"I said, \"Hello\"\""
However, this is causing the decoder to fail. Can anyone explain why? I'd like to know how to have double quotes in my JSON string.
Upvotes: 1
Views: 527
Reputation: 29106
That isn't a valid JSON string because you're not escaping the double quotes at the JSON level, only at the language level. With the first level of escape codes removed, your JSON string will look like this:
"I said, "Hello""
The double quote before "Hello" ends the string, hence why the error message says:
Unexpected token H in JSON at position 10
The JSON string you want is:
"I said, \"Hello\""
To produce this as a string literal in Elm you have to "double escape" the double quotes, which in practical terms just means adding an escaped backslash right before each of the escaped double quotes:
Decode.decodeString Decode.string "\"I said, \\\"Hello\\\"\""
Upvotes: 5