I am putting together a simple Python script, utilising the Python Machine Learning Services features in SQL Server. The goal is that my SQL server can ping an external API using a python script and read the results into variables.
I have successfully built the GET request, I am just struggling with getting the output of the API fed back into the database
what I have so far:
EXECUTE sp_execute_external_script @language = N'Python',
@script = N'
import requests
import pandas as pd
URL = "https://api.website.io/verify?email=dave@davidson.com&apikey=test_44fbcce85ba0f270273f4452bea2311de50f9c"
r = requests.get(url = URL)
data = r.text
print(data)
'
, @input_data_1 = N''
WITH RESULT SETS(([Col1] varchar(MAX) NOT NULL));
So print(data) gives me the following output in SSMS:
{"success":false,"message":"Invalid API key"}
But I don't want to print it, I want to read it into SQL. I don't really care how it comes out, so for now I've just specified a single column named "Col1" to hold the output, but I eventually need to load "False" and "Invalid API key" into two variables in my SQL Server Stored Procedure, so that I can use those values in the next step. If I have to parse those out of the JSON manually then I'm fine with that, but ideally they would come out as separate columns in the SQL output.
I've tried using r.json() in various permutations but I get a lot of different errors:
for SQL Server Machine Learning, I understand that I need to make a variable named "OutputDataSet" and that needs to be of the type "pandas dataframe", so I need to convert the JSON data held in "r" into a pandas dataframe. However everything I try on that front gives me cryptic error codes
so for example:
OutputDataSet = pd.DataFrame.from_dict(r.json(), orient="index")
gives: "Error in execution."
or
OutputDataSet = pd.DataFrame.from_dict(r.text, orient="index")
gives "AttributeError: 'str' object has no attribute 'values'"
Is it the syntax to convert JSON to a DataFrame that I'm not getting? Or is there an extra step or library needed to convert the output from the Requests lib into something that the Pandas lib will accept?