简体   繁体   English

使用Powershell和Excel将键和值添加到JSON

[英]Adding Key and value to JSON using Powershell and Excel

I have an excel file which I'm reading from to populate a Parameter ARM JSON file. 我有一个要从中读取的excel文件,用于填充参数ARM JSON文件。

The code I use is 我使用的代码是

$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"

I need the business value to be a "key" : "value" 我需要将业务价值作为“关键”:“价值”

In my excel I have the fields as below 在我的Excel中,我的字段如下

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

I can't work out how to add the "key" : "value" to the excel and get powershell to read it and populate the parameter sheet. 我不知道如何向excel中添加“ key”:“ value”并获得powershell来读取它并填充参数表。

The Client and user values are strings so work fine. 客户和用户值是字符串,因此可以正常工作。

I was hoping for some direction on where I'm going wrong 我一直在寻找我要去哪里的方向

Thanks in advance :) 提前致谢 :)

From what I understood from your screenshot, I think this might be of help to you: 从我对您的屏幕截图的了解来看,我认为这可能对您有所帮助:

$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

It will update your JSON to this: 它将您的JSON更新为:

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

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

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