简体   繁体   English

使用PowerShell嵌套JSON

[英]Nested JSON using PowerShell

My JSON looks like this: 我的JSON看起来像这样:

{
  "data": [
    {
      "name": "engagement",
      "period": "lifetime",
      "values": [
        {
          "value": 52
        }
      ],
      "title": "Engagement",
      "description": "Total number of likes and comments on the media object",
      "id": "1798601712/insights/engagement/lifetime"
    },
    {
      "name": "impressions",
      "period": "lifetime",
      "values": [
        {
          "value": 796
        }
      ],
      "title": "Impressions",
      "description": "Total number of times the media object has been seen",
      "id": "1798601712/insights/impressions/lifetime"
    }
  ]
}

What I managed to achieve at this moment: 我目前设法实现的目标:

"1798601712/insights/engagement/lifetime","engagement","52"
"1798601712/insights/impressions/lifetime","impressions","796"
"1798601712/insights/reach/lifetime","reach","422"

Using the following code: 使用以下代码:

$Ident = Import-Csv -Path ".\src\Process.txt" -Header $Header |
         Select-Object -Skip 2
foreach ($idka in $ident) {
    $sid = $idka.id
    $request_n = "https://api/"+ $sid +"/data=20190101&file=json"
    foreach($dane1 in $request_n) {
        Invoke-WebRequest $dane1 |
            ConvertFrom-Json |
            Select -ExpandProperty data |
            Select id, name, @{label = "values";Expression ={$_.values.value}} |
            Export-Csv $filename -NoTypeInformation -Append
    }
}

I need my csv to look like this: 我需要我的csv看起来像这样:

id  engagement  impressions reach
1798601712  52  796 422
1786717942  34  428 346
1787997335  29  376 281
1788199840  30  532 439
1788311007  48  1053 867
1788353947  28  609 497
1788403484  43  809 460

After expanding the data array group the nested objects by the ID you extract from the id field. 扩展data数组组后,将按照您从id字段中提取的ID嵌套对象。 For each group build a hashtable in which you map the values from each nested object to their name property. 对于每个组,构建一个哈希表,您可以在其中将每个嵌套对象的值映射到它们的name属性。 Create a custom object from the hashtable, then export the result to the output CSV. 从哈希表创建一个自定义对象,然后将结果导出到输出CSV。

...|
Select-Object -Expand data |
Group-Object { $_.id.Split('/')[0] } |
ForEach-Object {
    $prop = @{
        'id' = $_.Name
    }
    $_.Group | ForEach-Object {
        $prop[$_.name] = $_.values.value
    }
    New-Object -Type PSObject -Property $prop
} |
Select-Object id, engagement, impressions, reach |
Export-Csv $filename -NoType -Append

Note that with PowerShell v3 or newer you can use an ordered hashtable and the [PSCustomObject] type accelerator instead of New-Object , which would allow you to omit the last Select-Object (whose sole purpose is getting the output fields in the desired order). 请注意,在PowerShell v3或更高版本中,您可以使用有序哈希表和[PSCustomObject]类型的加速器,而不是New-Object ,这将允许您省略最后一个Select-Object (其唯一目的是按所需顺序获取输出字段) )。

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

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