Skip to content

JSON Field value returned as 'bytes'. #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
karamazi opened this issue Jul 4, 2016 · 0 comments · Fixed by #484
Closed

JSON Field value returned as 'bytes'. #482

karamazi opened this issue Jul 4, 2016 · 0 comments · Fixed by #484

Comments

@karamazi
Copy link

karamazi commented Jul 4, 2016

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?"}',)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant