简体   繁体   English

无法将 json 数组导入 azure 数据工厂中的 sql 表

[英]Unable to import json array to sql table in azure data factory

I have a JSON array similar to this.我有一个与此类似的 JSON 阵列。

 {
"first":
    {
    "heading":{
    "row":[
    {
    "@captain" :"dhoni",
    "@runs":"50"
    },
    {
    "@captain" :"Kohli",
    "@runs":"150"
    }
    ]
    }
    }
    }

But I am not sure how to add a sql script to import this JSON to my sql table.但我不确定如何添加 sql 脚本来将此 JSON 导入我的 sql 表。 Can anyone help on this任何人都可以帮忙吗

You can insert the rows extracted from the json returned by your API call with the help of for each activity and script activity.您可以在for each activityscript活动的帮助下插入从 API 调用返回的 json 中提取的行。

  • I have taken the given sample json as a parameter (Object type).我将给定的样本 json 作为参数(对象类型)。 First create a for each activity.首先为每个活动创建一个。 The value of items in for each activity will be as follows:每个活动的项目价值如下:
@pipeline().parameters.my_json['first']['heading']['row']
  • When we do this, for each iteration, the current item will be a row extracted from JSON that needs to be inserted into sql table.当我们这样做时,对于每次迭代,当前项目将是从 JSON 中提取的需要插入到 sql 表中的行。

在此处输入图像描述

  • Now inside for each, create a script activity.现在在每个内部,创建一个脚本活动。 Here, write a query to insert into the destination SQL table.在这里,编写一个查询以插入到目标 SQL 表中。 You can use the following as your query您可以使用以下内容作为您的查询
insert into dbo.player(captain,runs) values ('@{item()['@captain']}','@{item()['@runs']}')

在此处输入图像描述

Another approach using stored procedure:使用存储过程的另一种方法:

  • Create the following stored procedure in your database.在您的数据库中创建以下存储过程。
create  or  alter  procedure insert_player @json varchar(max)
as
begin
insert  into player SELECT  *  FROM OPENJSON(@json,'$.first.heading.row') with ([@captain] varchar(30),[@runs] varchar(30))
end
  • Create a single stored procedure activity in data factory pipeline.在数据工厂管道中创建单个存储过程活动。 Select the required linked service, the above created stored procedure and click import under stored procedure parameter . Select 所需的链接服务,上面创建的存储过程,点击存储过程参数下的导入
@string(pipeline().parameters.my_json)
  • NOTE: For me it is pipeline parameter, in your case it will be the lookup output that produced the above given sample JSON.注意:对我来说,它是管道参数,在您的情况下,它将是查找 output 生成上述给定示例 JSON。

在此处输入图像描述

  • When I debug the pipeline, it runs successfully and inserts the values into the respective table.当我调试管道时,它成功运行并将值插入到相应的表中。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM