J'ai MS SQL Server (2014) et ce dont j'ai besoin, c'est d'extraire un tas de métadonnées JSON d'une API web. Malheureusement, l'API se limite à renvoyer seulement 30 enregistrements (et j'ai 7400 enregistrements à tirer). Donc, j'allais simplement parcourir une série d'URL et importer les données, mais je me suis retrouvé coincé.Comment puis-je parcourir une liste d'URL pour extraire des données JSON?
J'ai importé des données JSON dans SQL Server dans le passé d'adapter cette méthode (Passing filename as variable in OPENROWSET(BULK filename)) et en utilisant parseJSON
Cette méthode fonctionne bien si j'ai les données locales à ma machine (par exemple, sur mon lecteur C), mais ne fonctionne pas lorsque j'utilise l'URL - je reçois l'erreur suivante:
(1 row(s) affected)
Msg 4861, Level 16, State 1, Line 1
Cannot bulk load because the file "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
Msg 8115, Level 16, State 2, Line 45
Arithmetic overflow error converting expression to data type nvarchar.
Comment puis-je fixer mon code pour travailler avec un fichier sur Internet?
-- SET @JSON_FILE= 'C:\Temp\SO.json' -- If I save the data on my harddrive the following code works.
SET @JSON_FILE= 'https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow'
/* http://www.sqlservercentral.com/Forums/Topic982066-338-1.aspx
Read the JSON file into the varchar variable. This is done via a bulk insert using the OPENROWSET() function. Because this stored proc is to be re-used with different JSON files, ideally you want to pass the JSON file path as a variable. However, because the OPENROWSET() function won't accept variables as a parameter, the command needs to be built as a string and then passed to the sp_executesql system stored procedure. The results are then passed back by an output variable.
*/
-- Setup varchar variable to be used to hold contents of JSON file.
DECLARE @txt varchar(max)
-- The command line
DECLARE @COMMAND NVARCHAR(MAX)
-- The definition of the parameters used within the command line
DECLARE @PARAM_DEF NVARCHAR(500)
-- The parameter used to pass the file name into the command
DECLARE @FILEVAR NVARCHAR(MAX)
-- The output variable that holds the results of the OPENROWSET()
DECLARE @JSON_OUT VARCHAR(MAX)
SET @FILEVAR = @JSON_FILE
SET @PARAM_DEF = N'@JSON_FILE NVARCHAR(MAX), @JSON_OUT VARCHAR(MAX) OUTPUT'
SET @COMMAND = N'SELECT @JSON_OUT = BulkColumn FROM OPENROWSET(BULK ''' + @JSON_FILE + ''', SINGLE_BLOB) ROW_SET';
EXEC sp_executesql @COMMAND, @PARAM_DEF, @JSON_FILE = @FILEVAR, @JSON_OUT = @txt OUTPUT;
--parseJSON function from https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/
SELECT * FROM parseJSON(@txt)
Cependant, ce n'est pas la partie JSON qui échoue, c'est la partie web. Le code fonctionne sur un fichier JSON local mais pas sur un hébergé sur le web ... – user918967
Ces suggestions de @marc_s nécessitent VisualStudio et/ou .NET - y at-il un autre outil? – user918967