【问题标题】:Adding Key and value to JSON using Powershell and Excel使用 Powershell 和 Excel 向 JSON 添加键和值
【发布时间】:2018-12-20 18:01:06
【问题描述】:

我有一个 Excel 文件,我正在读取该文件以填充参数 ARM JSON 文件。

我使用的代码是

$ws = $wb.Worksheets.Item(1)
$data = Get-Content -Path "$path\$jsonfile" -Raw | ConvertFrom-Json
$Row = 2
$col = 2  
$data.parameters.client.value = $ws.Cells.Item($Row, $col).Value()
$data.parameters.user.value = $ws.Cells.Item($Row, $col).Offset(1, 0).Value()
$data.parameters.business.value = $ws.Cells.Item($Row, $col).Offset(2, 0).Value()
$data.parameters.dev.value = $ws.Cells.Item($Row, $col).Offset(3, 0).Value()

$data | ConvertTo-Json -Depth 9 | % {
    [System.Text.RegularExpressions.Regex]::Unescape($_)
} | Set-Content -Path "$newpath\$JSONFile"

我需要商业价值成为“关键”:“价值”

在我的 excel 中,我有以下字段

Name           Value    Key
$client        Client1
$user          User1 
$business      Bus-key   bus-value

我不知道如何将“key”:“value”添加到 excel 并让 powershell 读取它并填充参数表。

客户端和用户值是字符串,所以可以正常工作。

我希望在哪里出错

提前致谢:)

【问题讨论】:

  • 您能否添加一个 JSON/Excel 文件示例以及您希望在 JSON/Excel 文件中看到的内容?
  • @DarkLite1 嗨,我已经上传了我正在努力实现和超越的图像:pastebin.com/embed_iframe/LW9RP80s

标签: excel azure powershell


【解决方案1】:

根据我从您的屏幕截图中了解到的情况,我认为这可能对您有所帮助:

$MyJson = '
{
    "FortinetTags": {
        "value":"6EB3B02F-50E5-4A3E-8CB8-2E129258317D"
    }
}' | ConvertFrom-Json

# We will create a hash table containing the required key/value pair
$MyJson.FortinetTags.value = @{'provider' = $MyJson.FortinetTags.value}

$MyJson | ConvertTo-Json

它会将您的 JSON 更新为:

{    
    "FortinetTags":  {                         
        "value":  {
            "provider":  "6EB3B02F-50E5-4A3E-8CB8-2E129258317D"                
        }                     
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2022-01-13
    • 2020-04-26
    • 2021-08-26
    • 2015-08-24
    • 2020-11-09
    • 2014-01-06
    相关资源
    最近更新 更多