I'm trying to loop through an array of objects within a JSON object @files, and insert each object from the array into a table, but I'm getting this error:
JSON text is not properly formatted. Unexpected character '.' is found at position 0.
The JSON was valid using JSONLint, so I know it's not the object that I declared, unless I'm wrong. When selecting the error it highlights this in the OPENJSON WITH() statement:
file_name NVARCHAR(100) '$.fileName',
ALTER PROCEDURE files_uploadAll
@document_id INT OUTPUT,
@files NVARCHAR(MAX)
/*
DECLARE @document_id INT
DECLARE @files NVARCHAR(MAX) = N'{
"files": [
{
"noteId": 1,
"documentTitle": "doc1",
"fileName": "doc1.pdf",
"fileExtension": "pdf",
"mimeType": "application/pdf",
"documentTypeCd": "MSA",
"userId": 1,
"url": "http://www.url.com"
},
{
"noteId": 2,
"documentTitle": "doc2",
"fileName": "doc2.doc",
"fileExtension": "doc",
"mimeType": "application/msword",
"documentTypeCd": "MSA",
"userId": 1,
"url": "http://www.url.com"
}
]
}';
EXECUTE files_uploadAll @files=@files, @document_id=@document_id OUTPUT
*/
AS
DECLARE @filesArray NVARCHAR(MAX)
SET @filesArray = (SELECT '$.files' FROM OPENJSON(@files))
DECLARE @filesList NVARCHAR(MAX), @i int
SELECT @i=0, @filesList = @filesArray
WHILE (@i < LEN(@filesList))
BEGIN
DECLARE @item NVARCHAR(MAX)
SELECT @item = SUBSTRING(@filesList, @i, CHARINDEX(',',@filesList,@i)-@i)
INSERT INTO documents
(note_id, document_title, file_name, file_extension, mime_type, document_type_cd, user_id, url)
SELECT note_id, document_title, file_name, file_extension, mime_type, document_type_cd, user_id, url
FROM OPENJSON(@item)
WITH (
note_id INT '$.noteId',
document_title NVARCHAR(100) '$.documentTitle',
file_name NVARCHAR(100) '$.fileName',
file_extension NVARCHAR(25) '$.fileExtension',
mime_type NVARCHAR(50) '$.mimeType',
document_type_cd CHAR(5) '$.documentTypeCd',
user_id int '$.userId',
url NVARCHAR(1000) '$.url'
)
SET @document_id=SCOPE_IDENTITY()
SET @i = CHARINDEX(',',@filesList,@i)+1
IF(@i = 0) SET @i = LEN(@filesList)
END