I need to read the below JSON file into Python with regex intact. I will be using the regular expressions in the program.
{
"Title": "Sample Compliance Check",
"Checks": {
"6": {
"+": ["^interfa.*", "^ip address 192\.168\.0"],
"description": "All interfaces with IP Address 192.168.0",
"action": "aaa new-model"
}
}
}
When I try to read this using the json module I get the error of invalid json.
json.decoder.JSONDecodeError: Invalid \escape:
I tried converting the backslash to double backslash
{
"Title": "Sample Compliance Check",
"Checks": {
"6": {
"+": ["^interfa.*", "^ip address 192\\.168\\.0"],
"description": "All interfaces with IP Address 192.168.0",
"action": "aaa new-model"
}
}
}
Now, it gets read in Python but I get the same output with double-backslashes.
Is there any way to encode regex in JSON and read it like it's encoded (in raw regex form)?
"^ip address 192\\.168\\.0"does only have single backslashes. They display as doubles so that Python can distinguish them from escape characters but it should be the regex you want ("^ip address 192\.168\.0")