【问题标题】:Convert JSON file to CSV using powershell in SSIS在 SSIS 中使用 powershell 将 JSON 文件转换为 CSV
【发布时间】:2017-05-30 08:15:54
【问题描述】:

我们有一个 JSON 格式(脚本)的 Web 服务,需要将其转换为 csv 文件并使用 powershell 拖放到文件共享位置。 所有这些都需要在运行 SSIS 包时捕获。 如何实现?

# Set the filename to have the date suffix.
$fileName = "2758_RS-DM_AIRData" + (Get-Date -Format yyyyMMdd_hh) + ".csv"
$location = "C:\ExtractFilePowerShellWebService\Data"
New-Item -ItemType file -Name $fileName -Path $location 

# delete if file exist
$FileName2 = (Join-Path $location $fileName)
if (Test-Path $FileName2) {
  Remove-Item $FileName2

}

# Capture data from AIR Web Service

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential("A", "B", "C")
$Json = $WebClient.Downloadstring("https://air.ciostage.accenture.com/ExternalServices/Application-service/Application(2758)")     
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength = [int]::MaxValue
$appCollection = @()
$appObj = $jsonserial.DeserializeObject($Json)
foreach($app in $appObj.value){

        $appObject = New-Object System.Object 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationId" -Value $app.ApplicationId 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationNm" -Value $app.ApplicationNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Tier" -Value $app.ServiceLevelOfferingNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Status" -Value $app.AppStatusNm
        #$appObject | Add-Member -MemberType NoteProperty -Name "Description" -Value $app.Description
        #$appObject | Add-Member -MemberType NoteProperty -Name "Acronym" -Value $app.Acronym
        #$appObject | Add-Member -MemberType NoteProperty -Name "Url" -Value $app.Url
        $appCollection += $appObject
}

#$appCollection | Export-Csv -Path "C:\ExtractFilePowerShellWebService\Data\2758_RS-DM_AIRData.csv" -notypeinformation

#extract data into CSV file.
$appCollection | Export-Csv  -Path (Join-Path $location $fileName) -notypeinformation 

当我运行脚本时,在提到的位置创建了 csv 文件,但它是空的

【问题讨论】:

  • 为什么它需要在 SSIS 包中?直接调用powershell脚本就行了
  • @Nick.McDermaid 因为 SSIS 包不仅仅是将一种格式转换为另一种格式。它将从很多不同的来源加载、转换、查找、组合和批量加载数据,并将其发送到很多不同的目标。并验证字段、大小等,以确保您不会意外截断数据,或者尝试传递比预期更少的列,或者将数字发送到字符串字段。尝试用 Powershell 脚本做同样的事情可能会......很有趣
  • @Aiswarya JSON 文件是什么样的?转换/加载平面对象应该很容易。转换嵌套对象需要进行转换,例如使用 Select 将它们展平或仅提取必要的字段
  • @Aiswarya 此外,SQL Server 2016 引入了 JSON 支持和查询。 也许您可以将单个 Json 行加载到表中,然后查询它们
  • 只是确定是否需要 SSIS。如果他们所做的只是将 JSON 转换为 CSV,那么这可能是矫枉过正。

标签: json powershell ssis


【解决方案1】:

我假设您的数据已经采用适合 csv 格式的格式,因为 Json 有很多 csv 无法显示的元素,我在此示例中使用 ConvertTo-Json 生成 Json 文件:

Get-Process | ConvertTo-Json -Depth 1 | Out-File .\file.json

注意-Depth 1,这意味着不会继承任何嵌套数组等。

然后我可以使用 ConvertFrom-Json 和以下内容将其转换为 csv:

(Get-Content .\file.json | ConvertFrom-Json) | Select Name,Handles,VM,PM | Export-Csv .\file.csv

【讨论】:

  • 换句话说,OP应该使用Convert-From
  • @PanagiotisKanavos 我不认为gc .. | convertfrom-json | export-csv 有效,直接通过管道传输时,covnertfrom-json 很奇怪。
  • 如果您解释说您已经可以转换为/从 Json、要使用哪些函数并向它们添加链接,那么答案将会更加有用。
猜你喜欢
  • 2021-03-31
  • 2015-08-23
  • 2017-09-18
  • 2017-09-21
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多