Short answer: Yes, you must.
The JSON type in SQLAlchemy is used to store the Python structure as JSON. It effectively performs:
database_value = json.dumps(python_value)
in storage and uses
python_value = json.loads(database_value)
You saved a string and it was converted to a JSON value. The fact that the string itself contained JSON was just a coincidence. Do not store JSON strings, keep Python values ββthat are JSON-serializable.
Quick demo to illustrate:
>>> print json.dumps({'foo': 'bar'}) {"foo": "bar"} >>> print json.dumps('This is a "string" with quotes!') "This is a \"string\" with quotes!"
Notice how the second example applies the same citation.
Use the JAON SQLAlchemy type to store additional structured data for an object; PostgreSQL gives you access to content in SQL expressions on the server side, and SQLAlchemy provides full access to content in the form of Python values ββon the Python side.
Keep in mind that you should always set the entire value anew to an object. Do not mutate the value inside it and expect SQLAlchemy to automatically detect this change; see the PostgreSQL JSON documentation.
source share