Unable to parse TAB in JSON files

From JSON standard:

Insignificant whitespace is allowed before or after any token. The
whitespace characters are: character tabulation (U+0009), line feed
(U+000A), carriage return (U+000D), and space (U+0020). Whitespace is
not allowed within any token, except that space is allowed in
strings.

It means that a literal tab character is not allowed inside a JSON string. You need to escape it as \t (in a .json-file):

{"My_string": "Foo bar.\t Bar foo."}

In addition if json text is provided inside a Python string literal then you need double escape the tab:

foo = '{"My_string": "Foo bar.\\t Bar foo."}' # in a Python source

Or use a Python raw string literal:

foo = r'{"My_string": "Foo bar.\t Bar foo."}' # in a Python source

Leave a Comment