R
R
Roman Sokolov2018-02-18 15:48:23
Python
Roman Sokolov, 2018-02-18 15:48:23

Why does a Python script return an array instead of JSON?

Hello.
I use an example from the Internet (the link could not be inserted):

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('[email protected]', 'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('[email protected]',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

In the output, a string is printed in JSON format:
{'password': 'very-secret', 'id': 1}
I have with my data, it is displayed, for example, like this:
('user45', 42344813)
I.e. like a regular array that I can access by index. What needs to be done to return the data in JSON format?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-02-18
@longclaps

You see Roma...
JavaScriptObjectNotation is not exactly native to python.
Yes, and for SQL databases too.
But for JavaScript it is native, yes. Maybe try there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question