0

My python app calls MSSQL 2017 function trough pyodbc and function query response is JSON formatted on the server side.

cursor.execute("SELECT dbo.fnInvoiceJSON(%s,%s);" % (posted,vat)) row = cursor.fetchone()

Response that returns in the app is a class 'pyodbc.Row like it should be. I can pass this response trough requests.post to other API call if i convert this to to string.

Is there any way to convert this response a accessible python dict / JSON object?

Petsku
  • 1
  • 2
  • If `fetchone()` returns a row object containing a single column then you can use `fetchval()` instead. Details [here](https://github.com/mkleehammer/pyodbc/wiki/Features-beyond-the-DB-API#fetchval). – Gord Thompson Mar 11 '18 at 13:52
  • Possible duplicate of [Rows returned by pyodbc are not JSON serializable](https://stackoverflow.com/questions/34715593/rows-returned-by-pyodbc-are-not-json-serializable) – Phonolog Mar 11 '18 at 15:20
  • Thanks @Gord Thompson. `fetchval()` doses the trick. Now return object is string and from there i can convert it to dict ´json.loads(row)` and so on. – Petsku Mar 11 '18 at 16:43

1 Answers1

0

As noted in the question, under pyodbc fetchone() returns a pyodbc.Row object and fetchall() returns an array of pyodbc.Row objects.

However, pyodbc also supports a fetchval() method that will return the value in the first column of the first row in the result set (similar to the ExecuteScalar method in C#). fetchval() returns the value as the corresponding Python type (e.g., string, None, etc.).

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418