Skip to content Skip to sidebar Skip to footer

Impossible To Store Json In Python With Single Un-escaped Backslash

I am creating a json body for a REST payload body like so: >>> j = json.loads('['foo', {'bar': ['to_be_replaced', 1.1, 1.0, 2]}]') >>> text = 'aaaa' + '\\' + 'bbb

Solution 1:

Yes, it is impossible -- by design.

A JSON parser is, by nature, supposed to emit only valid JSON. From RFC 8259, emphasis mine:

7. Strings

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. The hexadecimal letters A through F can be uppercase or lowercase. So, for example, a string containing only a single reverse solidus character may be represented as "\u005C".

Alternatively, there are two-character sequence escape representations of some popular characters. So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\".


Note the phrase "MUST be escaped" -- "MUST" is a formally-defined term-of-art; something which does not comply with a MUST requirement from the JSON specification is not allowed to call itself JSON.

In summary: A string containing only a literal backslash in your data may be encoded in JSON as "\u005c", or "\\". It may not be encoded as "\" (including that character as an unescaped literal).

Post a Comment for "Impossible To Store Json In Python With Single Un-escaped Backslash"