使用存储过程从 SQL 服务器数据库错误中的 Datafactory 同步输出的元数据:Json - 在位置 0 发现意外字符 'S'

Syncing Metadata output from Datafactory in SQL Server database error using stored procedure: Json - Unexpected character 'S' is found at position 0

我在尝试将 DataFactory 的元数据输出写入 SQL 服务器数据库时遇到此错误。

"errorCode": "2402",
"message": "Execution failed against SQL Server.
SQL error number: 13609.
Error Message: JSON text is not properly formatted. Unexpected character 'S' is found at position 0."

我正在 SQL 服务器数据库中使用存储过程。

元数据输出:

{
        "childItems": [
            {
                "name": "DemoFile1",
                "type": "File"
            },
            {
                "name": "DemoFile2",
                "type": "File"
            } ]
       "effectiveIntegrationRuntime": "AutoResolveIntegrationRuntime",
       "executionDuration": 0
}

程序代码:

CREATE PROCEDURE prod1
    @parameter1 NVARCHAR(max)
AS
BEGIN
    INSERT INTO [dbo].[Table1] ([name], [type])
        SELECT
            name, type
        FROM 
            OPENJSON(@parameter1)
                WITH (
                      name NVARCHAR(max) '$.name',
                      type NVARCHAR(max) '$.type'
                     ) AS jsonValues
END

TIA!

您的 json 结构有误,您的错误原因。实际上你在 json 中错过了 {} 应该如下所示:

{"childItems": [ { "name": "DemoFile1", "type": "File" }, { "name": "DemoFile2", "type": "File" } ]}

请尝试以下解决方案。

缺少一些东西:

  1. 花括号 { 和 }。
  2. 一个逗号],
  3. OPENJSON(@parameter1, '$.childItems')第二个参数。

您始终可以通过 T-SQL ISJSON() 函数检查它是否是格式正确的 JSON。

SQL

DECLARE @parameter1 NVARCHAR(max) = 
N'{
    "childItems": [
        {
            "name": "DemoFile1",
            "type": "File"
        },
        {
            "name": "DemoFile2",
            "type": "File"
        }
    ],
    "effectiveIntegrationRuntime": "AutoResolveIntegrationRuntime",
    "executionDuration": 0
}';

IF ISJSON(@parameter1) = 1
    SELECT name, type
    FROM OPENJSON(@parameter1, '$.childItems')
    WITH (
        name NVARCHAR(max) '$.name',
        type NVARCHAR(max) '$.type'
    ) AS jsonValues
ELSE
    THROW 50000,'JSON is not well-formed',1;

输出

+-----------+------+
|   name    | type |
+-----------+------+
| DemoFile1 | File |
| DemoFile2 | File |
+-----------+------+