-
Greetings, I am very new to Python and I am trying to insert data from a Python call to a SurveyMonkey api. I’ve gotten the data I want and have assigned the json returned to a python variable. I need to insert the entire json document in this variable to a SQL Server Column (nvarcharmax) so I can parse it further. I have tried the following code for my insert: conn = pyodbc.connect(‘Driver={SQL Server};’ cursor = conn.cursor() When I run this I get cursor.execute(’’' The python variable this json is stored in is called response_json and the SQL column is called Json. I don’t know why it thinks the input variable is a column when this is the python variable with the json values I want. I can print this variable and see the data I want. What am I doing wrong here? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Following additional reasearch, I have tried the following code: Sql_insert_query = “”"INSERT INTO SurveyList (Json) recordTuple = response_json This changes the error to the following: File “C:/Users/ITadmin/PycharmProjects/untitled/SurveyMonkey.py”, line 35, in <module> The data being returned is a json document from a REST API stored in a Python variable called response_json. I’m not sure what the correct parameter type is or how to specify it. Any help woul dbe appreciated. |
Beta Was this translation helpful? Give feedback.
-
Found somehting that worked. I’m posting the entire thing in case any newbie like me needs help to pull data from a REST API and get it into SQL as this took me awhile to get it going import requests client = requests.session() headers = { data = {} HOST = “https://api.surveymonkey.net” SURVEY_LIST_ENDPOINT = “/v3/surveys/” uri = “%s%s” % (HOST, SURVEY_LIST_ENDPOINT) response = client.get(uri, headers=headers) response_json = response.json() conn = pyodbc.connect(‘Driver={SQL Server};’ cursor = conn.cursor() |
Beta Was this translation helpful? Give feedback.
Found somehting that worked. I’m posting the entire thing in case any newbie like me needs help to pull data from a REST API and get it into SQL as this took me awhile to get it going
import requests
import pyodbc
import json
client = requests.session()
headers = {
“Authorization”: “bearer %s” % “apikey”,
“Content-Type”: “application/json”
}
data = {}
HOST = “https://api.surveymonkey.net”
SURVEY_LIST_ENDPOINT = “/v3/surveys/”
uri = “%s%s” % (HOST, SURVEY_LIST_ENDPOINT)
response = client.get(uri, headers=headers)
response_json = response.json()
conn = pyodbc.connect(‘Driver={SQL Server};’
‘Server=Servername;’
‘Database=DatabaseName;’
‘Trusted_Connection=yes;’)
cursor = conn.cursor()
curso…