0

Imagine I want the literal string "\u" stored in a JSON string.

This answer provides a nice visual overview of escape sequences in JSON, but it's not clear from its picture whether they are escaped at the same time or in order.

Consider the following JSON:

{
  "foo": "a\\u"
}

This ought - to me at least - be valid JSON, wherein foo contains the string a\u.

However \u is an escape sequence for Unicode characters and must - usually - be followed by four hexadecimal characters.

When I enter the same JSON through Json Parser Online, which I believe uses JSON.stringify yields the following error: "\u must be followed by 4 hexadecimal characters".

I also tried typing the following into both Firefox's and Chrome's consoles:

var json = JSON.stringify(eval("(" + '{"foo": "a\\u"}' + ")"));

And both yield an Unicode escape sequence error. Even the library superobject for Delphi yields the same error (or actually, it just throws a JSON parse error).

Looking through the RFC 7159 on strings, it does mention these escape sequences, but no mention on order.

It seems to me that the JSON standard does not consider the following sequence \\u, or at least, the parsers don't handle it well. If \\ is escaped first, \\u becomes \u and \u will be wrong. If \u is escaped first, then it yields an error before it even gets to \\.

This is a particularly problem for rich text in JSON, because \uc1 and \uc2 are common control sequences in RTF. The answer in question doesn't really touch on the subject, but the question also seems pretty much abandoned.

So a theoretical question would be: How are (and how should) escape sequences be escaped?

And a more practical question would be: How do I get the string literal \u into a JSON string beyond simply providing the Unicode sequence for either \ or u (e.g. a\u005Cu)?

Community
  • 1
  • 1
Svip
  • 2,958
  • 3
  • 22
  • 33

1 Answers1

1

What is the escape sequence order in JSON?

Left to right. \\u contains the string \u.

How do I get the string literal \u into a JSON string?

"\\u"

When I enter the same JSON through Json Parser Online, which I believe uses JSON.stringify yields the following error: "\u must be followed by 4 hexadecimal characters".

This appears to be a bug in that tool.

A straight parse with a browser's JSON.parse works fine:

// Note: The slashes have to be escaped to be put in the JS string literal.

var json = `{
  "foo": "a\\\\u"
}`;

console.log("JSON is: " + json);
var object = JSON.parse(json);
console.log("String in parsed object is: " + object.foo);
var json = JSON.stringify(eval("(" + '{"foo": "a\\u"}' + ")"));

While \ is an escape character in JSON, it is also an escape character in JavaScript string literals.

The JavaScript parser will convert "a\\u" to a\u before passing it to eval() which which will try to parse it as a JavaScript string literal (but an invalid one), before trying to convert it to JSON with JSON.stringify).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • You are right that "\\\\u" works through `eval()` and `JSON.stringify()`, but if I try that sequence through the JSON Parser Online, that still yields an error. And unfortunately, so does the superobject library I am using. Perhaps this is an error in these libraries? – Svip Mar 27 '17 at 10:32
  • To answer my own comment, it appears that superobject cannot properly escape the sequences. I've filed [an issue](https://github.com/hgourvest/superobject/issues/28), but I doubt it'll get fixed, so for now we are seeking a more 'hacky' solution. – Svip Mar 27 '17 at 10:49
  • Actually, to make matters worse, I was wrong! Superobject does work. So it must be something else that's causing the error. It's just that online tool that's broken. – Svip Mar 27 '17 at 11:25