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!

1

3 Answers 3

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}"'
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...!

1
  • Thanks Sidharthan, I think your solution is clean, and I do not need use replace(), just use \\ instead of \ in file. Commented Mar 19, 2014 at 7:29
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.

1
  • @Jerry: If you're reading JSON from a file, then either everything has already been handled for you, or the data source is broken. Commented Mar 19, 2014 at 7:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.