Python Forum

Full Version: Get Python json data i n SQL Server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings,

I am very new to Python and am looking for help to get a json document inserted into SQL so I can parse it. This will be an ongoing process that I'm hoping to do with SSIS. I've managed to get the content I want (from Survey Monkey API) using the following code. This is run within SQL Server SSMS and puts the data I want in the Surveys_Json variable. I need to get this outputted to an SQL Server database. I know SQL much more than I know Python. Once I get the document into SQL, I can parse it using the Json functions there. Any help would be appreciated.

EXECUTE sp_execute_external_script

@language = N'Python'

, @Script = N'

import requests

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)

surveylist = client.get(uri, headers=headers)

surveys_json = surveylist.json()

print(surveys_json)

'
It sounds like what you're asking is basically "how do I interact with an SQL Server database from a Python script?". If so, you'll need a third party library to do that; see this for details.