You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
document the "as_string" / "as_int" etc JSON methods not as "CAST" methods, since this is not accurate as to their purpose #11065
When using the SQLite driver, calling .as_string() on a JSON column in a query does not cast the result to a string. For example, if a have a table Table with a JSON column json, I may write a query such as select(Table.data['foo'].as_string()). If a row has the value {"foo": 1}, the result should be '1', a string. Instead, SQLAlchemy returns 1, an int.
I don't believe this is relevant to the current SQLite driver implementation, but SQLite's JSON access operators are not compatible with postgresql. In postgres, -> returns JSON while ->> returns text. In SQLite, -> returns text while ->> may return NULL, INT, REAL, or JSON.
i haven't looked carefully at the code above but that's the current behavioral contract, see how the test suite maps to the script above and see what's different
changed the title [-]SQLite: JSON.as_string does not cast to string[/-][+]document the "as_string" / "as_int" etc JSON methods not as "CAST" methods, since this is not accurate as to their purpose[/+]on Mar 1, 2024
The first operation says, "The JSON data in this element is encoded as a string; please return it as a string, and not as another JSON element". The second operation says, "please cast the data from this element to STRING".
If you have a JSON value that is of unknown type and you'd like it to be unconditionally string you would use CAST just like for any other SQL value that may already not be a string.
the documentation at https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.JSON can be amended to not use the word "CAST" in the way it does right now; "Casting JSON Elements to Other Types" should more accurately say, "Retrieving JSON Elements as their scalar type" or something like that. There should be a note: "note: the JSON type-specification methods should not be relied upon as general purpose CAST methods, as it is assumed the JSON values present already match the requested type. for true type cross-casting, e.g. strings as ints, ints as strings and similar, use the cast() function. A future release of SQLAlchemy may further enhance the type-specification methods to function as fully unconditional CAST methods".
that said, it looks like the PostgreSQL dialect, which is built around the assumption of a native JSON type, does apply a VARCHAR CAST for the as_string() operation. for non-native JSON dialects, which historically have been "all the rest", JSON elements were always returned as strings so no cast is needed.
this is not as yet a "bug" because no tests were written assuming someone wanted to retrieve JSON elements with a type other than the one specified without using CAST explicitly. however, since this is the apparent behavior of the other dialects, it likely should be added for SQLIte but I will open that as a separate use case since this was not the original purpose.
Understood the rationale, but having experience mainly with pg that use cast the behaviour is a bit strange.
I guess they should be intended more as type coerce that casts, meaning that the contract is "treat the value as this database type".
Given that pg is very rigid with the types, and a json str/boolean/float are not the same as its db str/boolean/float, so cast is needed otherwise it will raise type errors.
Overall though #11074 is probably what people expect
I was looking to not have unnecessary SQL / CAST verbosity in these expressions and to that degree it's pretty nice that SQLite JSON returns elements in their native type automatically. if we make as_XYZ() always assume a cross-cast, there will always be CAST verbiage.
Activity
CaselIT commentedon Feb 28, 2024
Hi,
Thanks for reporting. The sql being rendered is as follows:
Looking at the sql and the documentation of sqlite https://sqlite.org/json1.html#the_json_extract_function it seems that the implementation was taken from mysql but it's not correct.
It seems that it should render:
table.c.json['foo']->JSON_EXTRACT(example.json, '$."foo"')table.c.json['foo'].as_string()->CAST(JSON_EXTRACT(example.json, '$."foo"') AS TEXT)(of varchar, not sure it's any different in sqlite)does that work for you @matthew-mcallister ?
A PR would be apprecited for this
CaselIT commentedon Feb 28, 2024
as_integer()andas_float()also does not seem to work, whileas_boolean()does but it's not rendering a cast in sql@zzzeek is there a reason for this behaviour?
zzzeek commentedon Mar 1, 2024
all of as_integer(), as_float(), as_boolean() pass all tests for sqlite which we have here: https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/testing/suite/test_types.py#L1495 based on these fixtures https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/testing/suite/test_types.py#L1364
i haven't looked carefully at the code above but that's the current behavioral contract, see how the test suite maps to the script above and see what's different
[-]SQLite: JSON.as_string does not cast to string[/-][+]document the "as_string" / "as_int" etc JSON methods not as "CAST" methods, since this is not accurate as to their purpose[/+]zzzeek commentedon Mar 1, 2024
OK these methods make use of CAST, however they are not all-purpose "CAST" operations. that is, these two operations are not equivalent:
The first operation says, "The JSON data in this element is encoded as a string; please return it as a string, and not as another JSON element". The second operation says, "please cast the data from this element to STRING".
If you have a JSON value that is of unknown type and you'd like it to be unconditionally string you would use CAST just like for any other SQL value that may already not be a string.
the documentation at https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.JSON can be amended to not use the word "CAST" in the way it does right now; "Casting JSON Elements to Other Types" should more accurately say, "Retrieving JSON Elements as their scalar type" or something like that. There should be a note: "note: the JSON type-specification methods should not be relied upon as general purpose CAST methods, as it is assumed the JSON values present already match the requested type. for true type cross-casting, e.g. strings as ints, ints as strings and similar, use the cast() function. A future release of SQLAlchemy may further enhance the type-specification methods to function as fully unconditional CAST methods".
zzzeek commentedon Mar 1, 2024
that said, it looks like the PostgreSQL dialect, which is built around the assumption of a native JSON type, does apply a VARCHAR CAST for the
as_string()operation. for non-native JSON dialects, which historically have been "all the rest", JSON elements were always returned as strings so no cast is needed.this is not as yet a "bug" because no tests were written assuming someone wanted to retrieve JSON elements with a type other than the one specified without using CAST explicitly. however, since this is the apparent behavior of the other dialects, it likely should be added for SQLIte but I will open that as a separate use case since this was not the original purpose.
zzzeek commentedon Mar 1, 2024
this is #11074
CaselIT commentedon Mar 1, 2024
Understood the rationale, but having experience mainly with pg that use cast the behaviour is a bit strange.
I guess they should be intended more as type coerce that casts, meaning that the contract is "treat the value as this database type".
Given that pg is very rigid with the types, and a json str/boolean/float are not the same as its db str/boolean/float, so cast is needed otherwise it will raise type errors.
Overall though #11074 is probably what people expect
zzzeek commentedon Mar 1, 2024
I was looking to not have unnecessary SQL / CAST verbosity in these expressions and to that degree it's pretty nice that SQLite JSON returns elements in their native type automatically. if we make as_XYZ() always assume a cross-cast, there will always be CAST verbiage.
sqla-tester commentedon Mar 27, 2024
Federico Caselli has proposed a fix for this issue in the main branch:
Improve the documentation of json.as method https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5240
Improve the documentation of json.as method
zzzeek commentedon Aug 22, 2025
this is really just sqlite. the other dialects that do JSON all do the casting right now