4

I want to store some regex string in a json file, then use python to parse json file. however, as the regex backslash will be treated as invalid in json. so How should I do.

{
    "key" : "^https://www\.google\.com/search?.+",
    "value" : "google"
}

As you can see above, As regular expression, some fullstop (?.+) will be regex, some will be just fullstop (.), however, this json file will not be thought as valid. So how should I do.

Thanks!

Jerry YY Rain
  • 4,134
  • 7
  • 35
  • 52

3 Answers3

3

You need to use escape the backslashes for the regex, and then escape them again for the string processor:

>>> s = """{
...     "key" : "^https://www\\\\.google\\\\.com/search?.+",
...     "value" : "google"
... }"""
>>> json.loads(s)
{'key': '^https://www\\.google\\.com/search?.+', 'value': 'google'}

If you're going the other way, i. e. from a Python dictionary that contains a regex string to a JSON object, the encoder will handle this for you, but it's a good idea in general to use raw strings for regexes:

>>> s = """{
...     "key" : "^https://www\.google\.com/search?.+",
...     "value" : "google"
... }"""
>>> json.dumps(s)
'"{\\n    \\"key\\" : \\"^https://www\\\\.google\\\\.com/search?.+\\",\\n    \\"value\\" : \\"google\\"\\n}"'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

You need to escape the backslashes when generating the JSON string. Whatever tool you use to generate the JSON probably has that built in. For example, with the Python json module:

>>> print json.dumps({'asdf': r'\s+'})
{"asdf": "\\s+"}

Note that the output has two backslashes in it. When parsed, those will become one backslash.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • @Jerry: If you're reading JSON from a file, then either everything has already been handled for you, or the data source is broken. – user2357112 Mar 19 '14 at 07:02
1

Add one more back slash for each and every back slash .

        str=str.replace("\", "\\");

add above code into code before json usage..!

       {
          "key": "^https://www\\.google\\.com/search?.+",
          "value": "google"
       }

Its valid one..!

HOpe it helps...!

BlackPOP
  • 5,657
  • 2
  • 33
  • 49