Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json string decoding encountering invalid escape

Tags:

json

I am using simplejson to decode the following json string.

Here is a demo written in Python:

from simplejson import loads

loads("""["\s"]""")

The decoder will throw:

JSONDecodeError: Invalid \escape

How to cope with this? The expected output is:

["\\s"]
like image 590
xiaohan2012 Avatar asked May 07 '12 10:05

xiaohan2012


People also ask

How would you handle a bad JSON escape sequence?

Please use 4 back slashes to print a single back slash in the resultant JSON string. Two slashes turns into one slash and escapes C#'s string only, to escape JSON, you will need another two. Or use can use the @ string for simplification.

What are escape characters in JSON?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

What is JSON decoding?

Definition and Usage. The json_decode() function is used to decode or convert a JSON object to a PHP object.


1 Answers

"\s" is not a valid JSON escape string.

According to json.org, only the following escape are valid

  • \"
  • \\
  • /
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u four-hex-digits
like image 77
Demon Avatar answered Oct 10 '22 00:10

Demon