I can get this to work - but only if I am on SQL 2019 and I am in a database with a UTF-8 collation. It seems that when you select any of the SELECT_ xLOB options, CODEPAGE is ignored.
I was able to develop a workaround, though. For this to work, you need this format file:
9.0
1
1 SQLCHAR 0 0 "\r\n" 2 json ""
When you save it, make sure that you remove the leading spaces added by the forum software.
Here is a solution for SQL 2017 and up (replace the file names with your paths):
CREATE TABLE #temp (ident int IDENTITY,
txt nvarchar(MAX) NOT NULL)
BULK INSERT #temp FROM 'C:\temp\slask.json'
WITH (FORMATFILE ='C:\temp\slask.fmt', CODEPAGE=65001)
SELECT * FROM #temp
DECLARE [@](/users/na/?userId=b1b84c87-4001-0003-0000-000000000000) nvarchar(MAX)
SELECT [@](/users/na/?userId=b1b84c87-4001-0003-0000-000000000000) = string_agg(txt, '') WITHIN GROUP (ORDER BY ident)
FROM #temp
SELECT * FROM
OpenJson([@](/users/na/?userId=b1b84c87-4001-0003-0000-000000000000),'$')
With (
NativeName Nvarchar(100) '$.NativeBranchName'
)
go
DROP TABLE #temp
If you are on SQL 2016 and earlier, replace the SELECT with string_agg, with this SELECT:
SELECT [@](/users/na/?userId=b1b84c87-4001-0003-0000-000000000000) =
(SELECT txt AS [text()]
FROM #temp
ORDER BY ident
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)')