Using Microsoft SQL Server 2016 (RTM) Standard Edition, I am trying load a JSON into a SQL Server table using a SQL Server Agent Job, and get the following error:
JSON text is not properly formatted. Unexpected character '"' is found at position 508. [SQLSTATE 42000] (Error 13609).
But when I run it as T-SQL it inserts the data with out errors.
DECLARE @return_value int,
@responseText nvarchar(MAX),
@json nvarchar(MAX)
EXEC @return_value = [dbo].[HTTPRequest]
@URI = N'http://flexapi.foresightgps.com/ForesightFlexAPI.ashx',
@methodName = N'post',
@requestBody = N'{"method":"GetTagTempHistory","conncode":"PROVIDER","code":"USERNAME","wsuserid":"USERID" }',
@SoapAction = 'MSXML2.ServerXMLHTTP',
@UserName = N'USERNAME',
@Password = N'PASSWORD',
@responseText = @responseText OUTPUT
--SELECT @responseText as '@responseText';
SELECT @json= VALUE FROM OPENJSON(@responseText) WHERE [key]='ForesightFlexAPI';
INSERT INTO Localizado([TransactionID],[TrueTime],[Temp],[Name],[yLat],[xLong],[Speed],[Ignition],[Location])
SELECT [TransactionID],[TrueTime],[Temp],[Name],[yLat],[xLong],[Speed],[Ignition],[Location]
FROM OPENJSON(@json,'lax $.DATA')
WITH( TransactionID nvarchar(20) '$.TransactionID',
TrueTime NVARCHAR(50) '$.TrueTime',
Temp decimal(9,4) '$.Temp',
Name nvarchar(50) '$.Name',
yLat nvarchar(50) '$.yLat',
xLong nvarchar(50) '$.xLong',
Speed nvarchar(20) '$.Speed',
Ignition nvarchar(20) '$.Ignition',
Location nvarchar(500) 'lax $.Location'
)