从JSON文件获取数据并将其导出到XLSX表

时间:2019-07-19 07:49:13

标签: excel powershell

无法将JSON转换和导出为XLSX文件。

我设法使用以下脚本导出数据,但其中包含一些图像URL。您能帮我导出数据并进行过滤吗?

$jsonRequest = Invoke-WebRequest 
$jsonRequest.Content
$csv = $jsonRequest.Content | ConvertFrom-Json
$csv | Export-Csv -Path c:\1233last.csv

1 个答案:

答案 0 :(得分:0)

由于相同成分的物料可以具有多个值,因此所需的输出应按成分名称对这些物料求和。
下面的代码为此使用了一个小的辅助函数。 否则,它将仅遍历所有项目并返回PSObjects数组,其中包含导出到CSV所需的所有属性。

function Get-Amount ($ingredient,[string]$name) {
    # helper function to return the sum of the ingredient values
    [double]$amount = 0
    $ingredient | where-object {$_.name -eq $name} | ForEach-Object {
        # quick and dirty solution to convert the decimal comma into a decimal point
        $amount += [double]($_.amount.value -replace ',', '.')
    }
    if ($amount) {
         # If your Excel has trouble seeing the values as decimal numbers because
         # of possible culture differences, you can also output 
         # $amount.ToString("G", [System.Globalization.CultureInfo]::InvariantCulture) }
        return $amount
    } 
}

$jsonRequest = Invoke-WebRequest -Uri https://api.punkapi.com/v2/beers
$result = ($jsonRequest.Content | ConvertFrom-Json) | ForEach-Object { 
    # for convenience we store these in local variables
    $hops = $_.ingredients.hops
    $malt = $_.ingredients.malt
    [PSCustomObject]@{
        'Tagline' = $_.tagline
        'Name' = $_.name
        'Fuggles (hops)'                              = Get-Amount $hops 'Fuggles'
        'First Gold (hops)'                           = Get-Amount $hops 'First Gold'
        'Cascade (hops)'                              = Get-Amount $hops 'Cascade'
        'Maris Otter Extra Pale (malt)'               = Get-Amount $malt 'Maris Otter Extra Pale'
        'Caramalt (malt)'                             = Get-Amount $malt 'Caramalt'
        'Munich (malt)'                               = Get-Amount $malt 'Munich'
        'Yeast' = $_.ingredients.yeast
        'Acidulated Malt (malt)'                      = Get-Amount $malt 'Acidulated Malt'
        'Acidulated Malt for kettle souring (malt)'   = Get-Amount $malt 'Acidulated Malt for kettle souring'
        'Ahtanum (hops)'                              = Get-Amount $hops 'Ahtanum'
        'Amarillo (hops)'                             = Get-Amount $hops 'Amarillo'
        'Amber (malt)'                                = Get-Amount $malt 'Amber'
        'Bramling Cross (hops)'                       = Get-Amount $hops 'Bramling Cross'
        'Brown (malt)'                                = Get-Amount $malt 'Brown'
        'Carafa Special Malt Type 1 (malt)'           = Get-Amount $malt 'Carafa Special Malt Type 1'
        'Carafa Special Malt Type 3 (malt)'           = Get-Amount $malt 'Carafa Special Malt Type 3'
        'Centennial (hops)'                           = Get-Amount $hops 'Centennial'
        'Challenger (hops)'                           = Get-Amount $hops 'Challenger'
        'Chinook (hops)'                              = Get-Amount $hops 'Chinook'
        'Chocolate (malt)'                            = Get-Amount $malt 'Chocolate'
        'Citra (hops)'                                = Get-Amount $hops 'Citra'
        'Columbus (hops)'                             = Get-Amount $hops 'Columbus'
        'Columbus Extract (hops)'                     = Get-Amount $hops 'Columbus Extract'
        'Crystal (hops)'                              = Get-Amount $hops 'Crystal'
        'Crystal (malt)'                              = Get-Amount $malt 'Crystal'
        'Crystal 150 (malt)'                          = Get-Amount $malt 'Crystal 150'
        'Dark Crystal (malt)'                         = Get-Amount $malt 'Dark Crystal'
        'Dark Crystal 350-400 (malt)'                 = Get-Amount $malt 'Dark Crystal 350-400'
        'Extra Pale - Spring Blend (malt)'            = Get-Amount $malt 'Extra Pale - Spring Blend'
        'Extra Pale (malt)'                           = Get-Amount $malt 'Extra Pale'
        'Flaked Oats (malt)'                          = Get-Amount $malt 'Flaked Oats'
        'Galena (hops)'                               = Get-Amount $hops 'Galena'
        'Hersbrucker (hops)'                          = Get-Amount $hops 'Hersbrucker'
        'Honey (hops)'                                = Get-Amount $hops 'Honey'
        'Hop Extract (hops)'                          = Get-Amount $hops 'Hop Extract'
        'Lactose (hops)'                              = Get-Amount $hops 'Lactose'
        'Lager Malt (malt)'                           = Get-Amount $malt 'Lager Malt'
        'Magnum (hops)'                               = Get-Amount $hops 'Magnum'
        'Motueka (hops)'                              = Get-Amount $hops 'Motueka'
        'Mt.Hood (hops)'                              = Get-Amount $hops 'Mt.Hood'
        'Nelson Sauvin (hops)'                        = Get-Amount $hops 'Nelson Sauvin'
        'Pale Ale - Tipple (malt)'                    = Get-Amount $malt 'Pale Ale - Tipple'
        'Pale Ale (malt)'                             = Get-Amount $malt 'Pale Ale'
        'Peated Malt (malt)'                          = Get-Amount $malt 'Peated Malt'
        'Peppercorns (hops)'                          = Get-Amount $hops 'Peppercorns'
        'Propino Pale Malt (malt)'                    = Get-Amount $malt 'Propino Pale Malt'
        'Propino Pale Malt for kettle souring (malt)' = Get-Amount $malt 'Propino Pale Malt for kettle souring'
        'Roasted Barley (malt)'                       = Get-Amount $malt 'Roasted Barley'
        'Saaz (hops)'                                 = Get-Amount $hops 'Saaz'
        'Simcoe (hops)'                               = Get-Amount $hops 'Simcoe'
        'Smoked Malt (malt)'                          = Get-Amount $malt 'Smoked Malt'
        'Smoked Weyermann (malt)'                     = Get-Amount $malt 'Smoked Weyermann'
        'Sorachi Ace (hops)'                          = Get-Amount $hops 'Sorachi Ace'
        'Tomahawk (hops)'                             = Get-Amount $hops 'Tomahawk'
        'Waimea (hops)'                               = Get-Amount $hops 'Waimea'
        'Wheat (malt)'                                = Get-Amount $malt 'Wheat'
        'Wheat Malt (malt)'                           = Get-Amount $malt 'Wheat Malt'
        'Willamette (hops)'                           = Get-Amount $hops 'Willamette'
    }
}

$result | Export-Csv -Path c:\1233last.csv -NoTypeInformation