Bug #83513 JSON integer is casted to bytes instead of a python integer
Submitted: 25 Oct 2016 8:45 Modified: 9 Mar 2018 21:04
Reporter: Daniël van Eeden (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version: OS:Any
Assigned to: CPU Architecture:Any

[25 Oct 2016 8:45] Daniël van Eeden
Description:
JSON integer values are casted to bytes in python, not to integers.

How to repeat:
$ ./mysql_json_int.py 

Driver: mysql.connector 2.1.4
MySQL version: 5.7.16
—————————— ROW —————————————————————————————————————————————————————
VALUE      | JSON TYPE  | Python Type
b'1'       | INTEGER    | <class 'bytes'>
—————————— ROW —————————————————————————————————————————————————————
VALUE      | JSON TYPE  | Python Type
b'1'       | INTEGER    | <class 'bytes'>
$ cat mysql_json_int.py 
#!/usr/bin/python3
import mysql.connector

mysql_config = {
  'user': 'msandbox',
  'passwd': 'msandbox',
  'unix_socket': '/tmp/mysql_sandbox5716.sock',
}
driver  = mysql.connector

print("\nDriver: {} {}".format(driver.__name__, driver.__version__))

con = driver.connect(**mysql_config)
cur = con.cursor()
    
cur.execute('SELECT VERSION()')
print("MySQL version: {}".format(cur.fetchone()[0]))

cur.execute("""SELECT j->>'$.foo', JSON_TYPE(j->>'$.foo')
    FROM (SELECT json_object('foo', 1) AS j
    UNION ALL SELECT json_object('foo', '1')) jdata""")
              
for row in cur:
    print("—————————— ROW —————————————————————————————————————————————————————")
    print("VALUE      | JSON TYPE  | Python Type")
    print("%-10s | %-10s | %s" % (row[0], row[1].decode(), type(row[0])))

cur.close()
con.close()

Suggested fix:
Map JSON integers to Python integers.
[25 Oct 2016 10:57] Chiranjeevi Battula
Hello Daniël,

Thank you for the bug report and test case.
Verified this behavior on MySQL Connector/Python 2.1.4.

Thanks,
Chiranjeevi.
[25 Oct 2016 11:01] Chiranjeevi Battula
output:
Driver: mysql.connector 2.1.4
MySQL version: 5.7.16-log
—————————— ROW —————————————————————————————————————————————————————
VALUE      | JSON TYPE  | Python Type
b'1'       | INTEGER    | <class 'bytes'>
—————————— ROW —————————————————————————————————————————————————————
VALUE      | JSON TYPE  | Python Type
b'1'       | INTEGER    | <class 'bytes'>
>>>
[9 Mar 2018 21:04] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.11 release, and here's the changelog entry:

JSON integer values were cast to bytes in Python instead of integers.

Thank you for the bug report.
[2 May 2018 22:09] Philip Olson
Posted by developer:
 
This fix in 8.0.11 was backported to the upcoming MySQL Connector/Python 2.1.8 release.