Inserting JSON into MySQL using Python

use json.dumps(json_value) to convert your json object(python object) in a json string that you can insert in a text field in mysql

http://docs.python.org/library/json.html


To expand on the other answers:

Basically you need make sure of two things:

  1. That you have room for the full amount of data that you want to insert in the field that you are trying to place it. Different database field types can fit different amounts of data. See: MySQL String Datatypes. You probably want the "TEXT" or "BLOB" types.

  2. That you are safely passing the data to database. Some ways of passing data can cause the database to "look" at the data and it will get confused if the data looks like SQL. It's also a security risk. See: SQL Injection

The solution for #1 is to check that the database is designed with correct field type.

The solution for #2 is use parameterized (bound) queries. For instance, instead of:

# Simple, but naive, method.
# Notice that you are passing in 1 large argument to db.execute()
db.execute("INSERT INTO json_col VALUES (" + json_value + ")")

Better, use:

# Correct method. Uses parameter/bind variables.
# Notice that you are passing in 2 arguments to db.execute()
db.execute("INSERT INTO json_col VALUES %s", json_value)

Hope this helps. If so, let me know. :-)

If you are still having a problem, then we will need to examine your syntax more closely.