0
votes

I have a table "Employee" with json(nvarchar) column "Data". Following query works in SQL-Server.

select value from OpenJson(Employee.Data,'$.References')

How do I write sqlalchemy equivalent session.query for the above?

session.query(func.OPENJSON(Employee.Data,'$.References'))

I tried above, the sql-statement I got is, which I didn't expected.

select OPENJSON(Employee.Data,'$.References') from Employee

Any help here is much appreciated.

1
Maybe you can use the following form: session.query(func.JSON_VALUE(Employee.Data,'$.References'))? - rfkortekaas
@rfkortekaas - My Json value is {"References": [1,2,3,4]} I won't be able to use JSON_VALUE directly - Riyas

1 Answers

1
votes

I found answer for it after lot of trials. Following is working code,

from sqlalchemy import select, column, func

select(column('value')).select_from(func.OPENJSON(Employee.Data,'$.References'))