Background, I have a MSSQL server database that uses a specific instances, hence the connect string/engine to this database would look like this:
Engine(mssql+pyodbc://User:Password@servername\instance,5555/database?driver=ODBC+Driver+17+for+SQL+Server)
Note the single back slash between servername and instance.
My testing, with both as a string and a json types:
Env File 1:
myvar = '[{"A":"123","B":"456", "C":"A\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\B"}]
main.py File:
from dotenv import load_dotenv
import os
import json
if __name__ == '__main__':
load_dotenv('.env', override=True)
myv2 = os.getenv('myvar2')
myv = os.getenv('myvar')
myv3 = os.getenv('myvar3')
print(myv2)
print(myv)
print(myv3)
print('Json Outputs')
jmyv3 = json.loads(myv3)
print(f"{jmyv3=}")
jmyv = json.loads(myv)
print(f"{jmyv=}")
Generates an error Invalid \escape for both the string variable in .env and the json variable in .env
Now, let's modify .env file to this:
myvar = '[{"A":"123","B":"456", "C":"A\\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\\B"}]
The json variable returns but returns two back slashes and not one.
jmyv3=[{'A': '123', 'B': '456', 'C': 'A\\B'}]
And, the string version still errors with Invalid \escape
Now, for grins, I put three back slashes in .env
myvar = '[{"A":"123","B":"456", "C":"A\\\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\\\B"}]
This time, string returns but with two back slashes:
jmyv=[{'A': '123', 'B': '456', 'C': 'A\\B'}]
And, the json generates an error Invalid \escape
Is there anyway to get the 'C' part of the json string to just print a single back slash? Like this:
[{'A': '123', 'B': '456', 'C': 'A\B'}]
And, these variables are stored in Airflow with base64 encoding which my cause another layer of uncertainty with back slashes.