【问题标题】:Powershell export nested Json to CSV as rowPowershell 将嵌套的 Json 导出为 CSV 作为行
【发布时间】:2020-11-21 17:49:30
【问题描述】:

Json 包含以下结构,我想将值转换为行,然后使用 Powershell 将行插入到 CSV 文件中:

预期的 My CSV 输出将与下面的输出类似。我希望为每个 URL 和其他图像属性重复 GUID。非常感谢您调查请求并提供帮助。

CSV 输出样本

CSV outout example

enter code here
"_embedded": {
    "assets": [{
        "guid": "49EDBE70-2B28-3AD7-B993-7F68972BA1",
        "images": [{
            "URL": "https://www.rc.com/eqent_images/2020183/thumb/12014855_1.jpg",
            "imageSize": "thumb",
            "imageType": "OTHER",
            "angleNo": 1,
            "photoGuid": "aeb75f64-9005-472-a85a-f857a7b8e27"
        }, {
            "URL": "https://www.rc.com/eqt_images/2020183/largest/12014855_1.jpg",
            "imageSize": "largest",
            "imageType": "OTHER",
            "angleNo": 1,
            "photoGuid": "aeb75f64-9005-47f2-a85a-f857a7b8e27"
        }],
        "_links": {
            "self": {
                "href": "http://www.rc.com/asset-images/guid/49EDBE70-2B9-3AD7-B993-7F670472BA10?imageSize=thumbnail&imageSize=largest&angleNo=1{&imageType}",
                "templated": true
            }
        }
    }, {
        "guid": "D7C5B69C-083C-7598-2762-B57D64B4D82",
        "images": [{
            "URL": "https://www.rc.com/equipment_images/2090183/thumb/12050336_1.jpg",
            "imageSize": "thumb",
            "imageType": "OTHER",
            "angleNo": 1,
            "photoGuid": "11e196a8-02d3-42ae-9620-1a992516a50"
        }, {
            "URL": "https://www.rc.com/eqnt_images/2020183/largest/12050336_1.jpg",
            "imageSize": "largest",
            "imageType": "OTHER",
            "angleNo": 1,
            "photoGuid": "11e196a8-02d3-42ae-9620-1a7f251a950"
        }],
        "_links": {
            "self": {
                "href": "http://www.rc.com/asset-images/guid/D7C8969C-083C-7598-2762-B57D6994D82E?imageSize=thumbnail&imageSize=largest&angleNo=1{&imageType}",
                "templated": true
            }
        }
    }]
},
"_links": {
    "self": {
        "href": "http://www.rc.com/assetdata-imagescall/search/byGUIDSString?imageSize=thumbnail&imageSize=largest&angleNo=1&page=0&size=1500"
    }
},
"page": {
    "size": 1500,
    "totalElements": 2,
    "totalPages": 1,
    "number": 0
}

}

【问题讨论】:

标签: json powershell csv nested export


【解决方案1】:

如果您有一个名为 j.json 的格式正确的 JSON,您可能会执行以下操作:

# Read JSON as a PowerShell Object
$obj = Get-Content j.json | ConvertFrom-Json

# Loop through each asset as it contains the GUIDs and Images
$output = $obj._embedded.assets | Foreach-Object {
    # Keep track of Guid as we loop through images
    $guid = $_.Guid
    # Loop through the images to find each URL
    # Use calculated property to create GUID property
    $_.Images | Select-Object @{n='GUID';e={$guid}},URL
}

# Convert to CSV on console only. Not needed if outputting to file
$output | ConvertTo-Csv -NoType

# Output to CSV file
$output | Export-Csv -Path output.csv -NoType

有关计算属性的信息,请参阅Select-Object

【讨论】:

  • 完美的答案,这就像一个魅力!这正是我所需要的。感谢您的快速响应。请注意:我收到错误“ConvertFrom-Json : Invalid object pass in, ':' or '}' expected. (1): {” 可能是 Json 格式不正确。我修改了它'$obj = Get-Content $sourceFilePath -Raw | ConvertFrom-Json' 其他一切都一样。
猜你喜欢
  • 2021-01-08
  • 2019-08-01
  • 2020-01-21
  • 2019-05-08
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多