Bug #83516 Result of JSON_TYPE is bytearray instead of str
Submitted: 25 Oct 2016 9:21 Modified: 9 Mar 2018 22:36
Reporter: Daniël van Eeden (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:2.1.4 OS:Any
Assigned to: CPU Architecture:Any
Tags: json

[25 Oct 2016 9:21] Daniël van Eeden
Description:
I would expect the result of JSON_TYPE() to be str instead of bytearray

Not that the type is 'bytearray', not 'bytes'

Related:
Bug #83513 	JSON integer is casted to bytes instead of a python integer

How to repeat:
$ ./mysql_json_type.py 

Driver: mysql.connector 2.1.4
MySQL version: 5.7.16
VALUE: {"foo": "bar"}, TYPE: <class 'str'>
VALUE: bytearray(b'OBJECT'), TYPE: <class 'bytearray'>
VALUE: foo, TYPE: <class 'str'>
$ cat mysql_json_type.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, JSON_TYPE(j), 'foo'
    FROM (SELECT json_object('foo', 'bar') AS j) jdata""")
              
for row in cur:
    for col in row:
        print("VALUE: %s, TYPE: %s" % (col, type(col)))

cur.close()
con.close()
[25 Oct 2016 11:03] 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:04] Chiranjeevi Battula
output:
Driver: mysql.connector 2.1.4
MySQL version: 5.7.16-log
VALUE: {"foo": "bar"}, TYPE: <class 'str'>
VALUE: bytearray(b'OBJECT'), TYPE: <class 'bytearray'>
VALUE: foo, TYPE: <class 'str'>
>>>
[9 Mar 2018 22:36] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.11 release, and here's the changelog entry:

The result from JSON_TYPE() was returned as a bytearray instead of a
string. The returned value is now a string decoded using the current character
set, or as a bytearray if this conversion fails.

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.