简体   繁体   English

将 Azure 数据工厂中的 JSON 转换为 CSV

[英]Convert JSON to CSV in Azure Data Factory

I have a problem with converting JSON to CSV.我在将 JSON 转换为 CSV 时遇到问题。 JSON looks like that: JSON 看起来像这样:

{
  "Key1": "Value1",
  "Key2": "Value3",
  "Key3": "Value2",
  ...
}

And I expect the CSV in this format:我希望 CSV 采用这种格式:

Key, Value
Key1, Value1
Key2, Value2
Key3, Value3
...

As you see keys and values are stored in column.如您所见,键和值存储在列中。 Is it possible to do that in data flow in Azure Data Factory?是否可以在 Azure 数据工厂的数据流中做到这一点?

You can make use of flatten transformation in data flow.您可以在数据流中使用展平转换。 Please check the below Microsoft documentation page.请查看以下 Microsoft 文档页面。

https://docs.microsoft.com/en-us/azure/data-factory/data-flow-flatten https://docs.microsoft.com/en-us/azure/data-factory/data-flow-flatten

You can achieve it using Azure data factory Data flow Unpivot transformation.您可以使用 Azure 数据工厂数据流Unpivot转换来实现它。

Please see the below repro details.请参阅下面的复制详细信息。

Input:输入:

在此处输入图像描述

Data flow:数据流:

  1. Add Source and connect it to the JSON Input file.添加Source并将其连接到 JSON 输入文件。 In source options under JSON settings, select the document form as Single document .在 JSON 设置下的 source options 中,select 文档形式为Single document

在此处输入图像描述

Source Data preview:源数据预览:

在此处输入图像描述

  1. Add derived column after source to add a dummy column with a constant value to later use it in Unpivot transformation.在源之后添加derived column以添加具有量值的虚拟列,以便稍后在 Unpivot 转换中使用它。

在此处输入图像描述

Derived column Data preview:派生列数据预览:

在此处输入图像描述

  1. Add Unpivot transformation after Derived column, to convert columns to rows.在 Derived 列之后添加Unpivot转换,以将列转换为行。 Under Unpivot settings在 Unpivot 设置下

    a) Ungroup by: Select dummy column. a)取消分组: Select 虚拟列。

    b) Unpivot key: Give a name to an unpivot column (Ex: Key), type as 'string'. b) Unpivot key:为 unpivot 列命名(例如:Key),键入“字符串”。

    c) Unpivoted columns: Give a column name for the data in unpivoted columns. c)未透视列:为未透视列中的数据指定列名。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Unpivot Data preview:反透视数据预览:

在此处输入图像描述

  1. Add Select transformation after Unpivot to remove the dummy column and select only required columns to the output.在 Unpivot 之后添加Select转换以删除虚拟列,并且 select 仅需要列到 output。

在此处输入图像描述

Select Data preview: Select 数据预览:

在此处输入图像描述

  1. Add sink transformation at the end and connect to sink delimited dataset .在末尾添加接收sink转换并连接到接收器分隔数据集 In Settings , if you select the File name option as Output to single file , you can provide the sink file name.Settings中,如果您将 select File name option as Output to single file ,您可以提供接收器文件名。

在此处输入图像描述

Sink Data preview:接收数据预览:

在此处输入图像描述

Output: Output:

在此处输入图像描述

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

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