将嵌套的 JSON 数组转换为 CSV 文件中的单独列

问题描述 投票:0回答:2

我有一个如下所示的 JSON 文件:

{
    "id": 10011,
    "title": "Test procedure",
    "slug": "slug",
    "url": "http://test.test",
    "email": "[email protected]",
    "link": "http://test.er",
    "subject": "testing",
    "level": 1,
    "disciplines": [
      "discipline_a",
      "discipline_b",
      "discipline_c"
    ],
    "areas": [
      "area_a",
      "area_b"
    ]
  },

我尝试使用以下命令将其转换为 CSV 文件:

(Get-Content "PATH_TO\test.json" -Raw | ConvertFrom-Json)| Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO\test.csv"

但是,对于学科和领域,我在生成的 CSV 文件中获取 System.Object[]。

有没有办法将所有这些嵌套值作为单独的列放入 CSV 文件中,例如area_1、area_2 等。学科也是如此。

json powershell csv data-conversion flatten
2个回答
11
投票

2017-11-20,完全重写功能以提高性能并添加

-ArrayBase
功能以及对PSStandardMembers和分组对象的支持。

展平对象

递归地展平包含数组、哈希表和(自定义)对象的对象。所提供的对象的所有添加属性都将与其余对象对齐

需要 PowerShell 版本 2 或更高版本。

cmdlet

Function Flatten-Object {                                       # Version 00.02.12, by iRon
    [CmdletBinding()]Param (
        [Parameter(ValueFromPipeLine = $True)][Object[]]$Objects,
        [String]$Separator = ".", [ValidateSet("", 0, 1)]$Base = 1, [Int]$Depth = 5, [Int]$Uncut = 1,
        [String[]]$ToString = ([String], [DateTime], [TimeSpan]), [String[]]$Path = @()
    )
    $PipeLine = $Input | ForEach {$_}; If ($PipeLine) {$Objects = $PipeLine}
    If (@(Get-PSCallStack)[1].Command -eq $MyInvocation.MyCommand.Name -or @(Get-PSCallStack)[1].Command -eq "<position>") {
        $Object = @($Objects)[0]; $Iterate = New-Object System.Collections.Specialized.OrderedDictionary
        If ($ToString | Where {$Object -is $_}) {$Object = $Object.ToString()}
        ElseIf ($Depth) {$Depth--
            If ($Object.GetEnumerator.OverloadDefinitions -match "[\W]IDictionaryEnumerator[\W]") {
                $Iterate = $Object
            } ElseIf ($Object.GetEnumerator.OverloadDefinitions -match "[\W]IEnumerator[\W]") {
                $Object.GetEnumerator() | ForEach -Begin {$i = $Base} {$Iterate.($i) = $_; $i += 1}
            } Else {
                $Names = If ($Uncut) {$Uncut--} Else {$Object.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames}
                If (!$Names) {$Names = $Object.PSObject.Properties | Where {$_.IsGettable} | Select -Expand Name}
                If ($Names) {$Names | ForEach {$Iterate.$_ = $Object.$_}}
            }
        }
        If (@($Iterate.Keys).Count) {
            $Iterate.Keys | ForEach {
                Flatten-Object @(,$Iterate.$_) $Separator $Base $Depth $Uncut $ToString ($Path + $_)
            }
        }  Else {$Property.(($Path | Where {$_}) -Join $Separator) = $Object}
    } ElseIf ($Objects -ne $Null) {
        @($Objects) | ForEach -Begin {$Output = @(); $Names = @()} {
            New-Variable -Force -Option AllScope -Name Property -Value (New-Object System.Collections.Specialized.OrderedDictionary)
            Flatten-Object @(,$_) $Separator $Base $Depth $Uncut $ToString $Path
            $Output += New-Object PSObject -Property $Property
            $Names += $Output[-1].PSObject.Properties | Select -Expand Name
        }
        $Output | Select ([String[]]($Names | Select -Unique))
    }
}; Set-Alias Flatten Flatten-Object

语法

<Object[]> Flatten-Object [-Separator <String>] [-Base "" | 0 | 1] [-Depth <Int>] [-Uncut<Int>] [ToString <Type[]>]

或:

Flatten-Object <Object[]> [[-Separator] <String>] [[-Base] "" | 0 | 1] [[-Depth] <Int>] [[-Uncut] <Int>] [[ToString] <Type[]>]

参数

-Object[] <Object[]>

要压平的对象(或多个对象)。

-Separator <String>
(默认:
.

递归属性名称之间使用的分隔符。 .

-Depth <Int>
(默认:
5

展平递归属性的最大深度。任何负值都将导致无限深度,并可能导致无限循环。

-Uncut <Int>
(默认:
1

未切割更多对象属性的 object 迭代次数将仅限于
DefaultDisplayPropertySet
。任何负值都会揭示所有对象的所有属性。

-Base "" | 0 | 1
(默认:
1

嵌入数组的第一个索引名称:

  • 1
    ,数组将从 1 开始:
    <Parent>.1
    <Parent>.2
    <Parent>.3
    、...
  • 0
    ,数组将从 0 开始:
    <Parent>.0
    <Parent>.1
    <Parent>.2
    、...
  • ""
    ,数组中的第一项将是未命名的,并且后面跟着 1:
    <Parent>
    <Parent>.1
    <Parent>.2
    、...

-ToString <Type[]= [String], [DateTime], [TimeSpan]>

值类型列表(默认
[String], [DateTime], [TimeSpan]
)将被转换为字符串而不是进一步扁平化。例如。
[DateTime]
可以使用
Date
Day
DayOfWeek
等附加属性进行展平,但会转换为单个 (
String
) 属性。

注:
参数

-Path
供内部使用,但只能用于前缀属性名称。

示例

回答具体问题:

(Get-Content "PATH_TO\test.json" -Raw | ConvertFrom-Json) | Flatten-Object | Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO\test.csv"

结果:

{
    "url":  "http://test.test",
    "slug":  "slug",
    "id":  10011,
    "link":  "http://test.er",
    "level":  1,
    "areas.2":  "area_b",
    "areas.1":  "area_a",
    "disciplines.3":  "discipline_c",
    "disciplines.2":  "discipline_b",
    "disciplines.1":  "discipline_a",
    "subject":  "testing",
    "title":  "Test procedure",
    "email":  "[email protected]"
}

对更复杂的自定义对象进行压力测试:

New-Object PSObject @{
    String    = [String]"Text"
    Char      = [Char]65
    Byte      = [Byte]66
    Int       = [Int]67
    Long      = [Long]68
    Null      = $Null
    Booleans  = $False, $True
    Decimal   = [Decimal]69
    Single    = [Single]70
    Double    = [Double]71
    Array     = @("One", "Two", @("Three", "Four"), "Five")
    HashTable = @{city="New York"; currency="Dollar"; postalCode=10021; Etc = @("Three", "Four", "Five")}
    Object    = New-Object PSObject -Property @{Name = "One";   Value = 1; Text = @("First", "1st")}
} | Flatten

结果:

Double               : 71
Decimal              : 69
Long                 : 68
Array.1              : One
Array.2              : Two
Array.3.1            : Three
Array.3.2            : Four
Array.4              : Five
Object.Name          : One
Object.Value         : 1
Object.Text.1        : First
Object.Text.2        : 1st
Int                  : 67
Byte                 : 66
HashTable.postalCode : 10021
HashTable.currency   : Dollar
HashTable.Etc.1      : Three
HashTable.Etc.2      : Four
HashTable.Etc.3      : Five
HashTable.city       : New York
Booleans.1           : False
Booleans.2           : True
String               : Text
Char                 : A
Single               : 70
Null                 :

展平分组对象:

$csv | Group Name | Flatten | Format-Table 
# https://stackoverflow.com/a/47409634/1701026

展平常见物体:

(Get-Process)[0] | Flatten-Object

或者对象列表(数组):

Get-Service | Flatten-Object -Depth 3 | Export-CSV Service.csv

请注意,以下命令需要数小时才能计算:

Get-Process | Flatten-Object | Export-CSV Process.csv

为什么?因为它会生成一个包含数百行和数千列的表。因此,如果您想将其用于展平过程,最好限制行数(使用

Where-Object
cmdlet)或列数(使用
Select-Object
cmdlet)。


4
投票

CSV 转换/导出 cmdlet 无法“展平”对象,我可能会遗漏一些东西,但我知道没有办法使用内置 cmdlet 或功能来做到这一点。 如果您可以保证

disciplines
areas
始终具有相同数量的元素,则可以通过使用带有派生属性的
Select-Object
来简化它:

$properties=@('id','title','slug','url','email','link','subject','level',
    @{Name='discipline_1';Expression={$_.disciplines[0]}}
    @{Name='discipline_2';Expression={$_.disciplines[1]}}
    @{Name='discipline_3';Expression={$_.disciplines[2]}}
    @{Name='area_1';Expression={$_.areas[0]}}
    @{Name='area_2';Expression={$_.areas[1]}}
)
(Get-Content 'PATH_TO\test.json' -Raw | ConvertFrom-Json)| Select-Object -Property $properties | Export-CSV -NoTypeInformation -Path 'PATH_TO\test.csv'

但是,我假设

disciplines
areas
对于每个记录来说都是可变长度的。在这种情况下,您将必须循环输入并提取学科和领域的最高计数值,然后动态构建属性数组:

$inputData = Get-Content 'PATH_TO\test.json' -Raw | ConvertFrom-Json
$counts = $inputData | Select-Object -Property     @{Name='disciplineCount';Expression={$_.disciplines.Count}},@{Name='areaCount';Expression={$_.areas.count}}
$maxDisciplines = $counts | Measure-Object -Maximum -Property disciplineCount | Select-Object -ExpandProperty     Maximum
$maxAreas = $counts | Measure-Object -Maximum -Property areaCount | Select-Object -ExpandProperty Maximum

$properties=@('id','title','slug','url','email','link','subject','level')

1..$maxDisciplines | % {
  $properties += @{Name="discipline_$_";Expression=[scriptblock]::create("`$_.disciplines[$($_ - 1)]")}
}

1..$maxAreas | % {
  $properties += @{Name="area_$_";Expression=[scriptblock]::create("`$_.areas[$($_ - 1)]")}
}

$inputData | Select-Object -Property $properties | Export-CSV -NoTypeInformation -Path 'PATH_TO\test.csv'

此代码尚未经过全面测试,因此可能需要一些调整才能 100% 工作,但我相信这些想法是可靠的 =)

© www.soinside.com 2019 - 2024. All rights reserved.