Context: I have a JSON document with the following form saved to a column in a temp table in SQL Server
[{"File": {"File_Name": "SCAN_6X_AHMC_AAPC_837I_11182019_11242019.txt", "Last_Modified": "Lastmodified:20191125.121049", "File_Size": "Filesize:7196"}}, {"File": {"File_Name": "SCAN_6X_AHMC_AAPC_837P_11182019_11242019.txt", "Last_Modified": "Lastmodified:20191125.121017", "File_Size": "Filesize:3949"}}]
Question: I am trying to extract the file names, modified dates and file sizes from the JSON using T-SQL's OPENJSON function. With the code block below I'm pulling NULL values in the fields I'm trying to get at. Why? And how can I fix this?
SELECT ##jsondump.my_json, jsn.[file_name], jsn.last_modified, jsn.file_size
FROM ##jsondump
OUTER APPLY (
SELECT * FROM OPENJSON(##jsondump.my_json, '$.File')
WITH (
[file_name] NVARCHAR(50) '$.File_Name',
last_modified NVARCHAR(50) '$.Last_Modified',
file_size NVARCHAR(50) '$.File_Size'
)
) AS jsn
Similarly this method also doesnt work. Thoughts/suggestions?
DECLARE @json NVARCHAR(MAX)
SET @json = (SELECT my_json FROM ##jsondump)
SELECT json_column.*,
JSON_VALUE([value], '$.File_Name') As [File_Name],
JSON_VALUE([value], '$.Last_Modified') As Last_Modified,
JSON_VALUE([value], '$.File_Size') As File_Size
FROM OPENJSON(@json) as json_column