Skip to content

JSON Field value returned as 'bytes'. #482

Description

@karamazi

With PyMySQL 0.7.5 (current master branch) and MySQL Server 5.7.13 When querying for JSON field, expected result would be a unicode string (or preferably json object). Instead bytes are returned.

Example code:

import pymysql
import json

db_config = {
    "host": "mysql.dev",
    "port": 3306,
    "user": "root",
    "password": "pass",
    "autocommit": True
}

with pymysql.connect(**db_config) as cur:
    cur.execute("CREATE DATABASE IF NOT EXISTS `json_test` /*!40100 DEFAULT CHARACTER SET utf8 */")
    cur.execute("""
        CREATE TABLE IF NOT EXISTS `json_test`.`text_table` (
        `text_field` text NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
    cur.execute("""
        CREATE TABLE IF NOT EXISTS `json_test`.`json_table` (
        `json_field` JSON NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")

    cur.execute("INSERT INTO `json_test`.`text_table` VALUES (%s)", "I am a unicode text")

    json_data = json.dumps({"some_key": "Am I a unicode string?"})
    cur.execute("INSERT INTO `json_test`.`json_table` VALUES (%s)", json_data)

    cur.execute("SELECT * FROM `json_test`.`text_table` LIMIT 1")
    print(cur.fetchone())

    cur.execute("SELECT * FROM `json_test`.`json_table` LIMIT 1")
    print(cur.fetchone())

    cur.execute("DROP DATABASE `json_test`")

Result:

('I am a unicode text',)
(b'{"some_key": "Am I a unicode string?"}',)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions