修改Powershell 4中现有的JSON文件

修改Powershell 4中现有的JSON文件,json,powershell,Json,Powershell,我已经看到了很多关于这方面的问题,但是没有一个答案是有效的(如果我错了,请纠正我) 我创建了一个查询API并将结果保存为json文件的函数。 但是,我想修改保存的文件 powershell代码: $search = "" function searchMtgApi($searchName){ $uriSafeName = [uri]::EscapeDataString($searchName) $res = Invoke-WebRequest "https://api.mag

我已经看到了很多关于这方面的问题,但是没有一个答案是有效的(如果我错了,请纠正我)

我创建了一个查询API并将结果保存为json文件的函数。
但是,我想修改保存的文件

powershell代码:

$search = ""


function searchMtgApi($searchName){
    $uriSafeName = [uri]::EscapeDataString($searchName)
    $res = Invoke-WebRequest "https://api.magicthegathering.io/v1/cards?name=$uriSafeName" -UseBasicParsing
    $resJson = $res.content | ConvertFrom-Json
    $resJson.cards | Format-Table
    $resJson.cards | Select name, setName, "quantity" | ConvertTo-Json | Out-File "D:\Magic the Gathering\$searchName.json"
}

while ($search -ne "exit"){
    Write-Host @'

To exit, type "Exit".

'@
    $search = Read-Host "Search Magic the Gathering.IO by card name"
    if($search -ne "exit"){
          searchMtgApi $search
    }
}
生成Json(搜索卡片“Pounder”)

我想做的是加载文件并修改特定集合的“数量”。
谁能给我指出正确的方向吗

  • 更新-
我将从文件中获取json内容:

function updateJsonFile($jsonfile){
    $resJson = Get-Content "$jsonfile.json" | ConvertFrom-Json 
    #Edit Quantity here
    $resJson | ConvertFrom-Json | Format-Table
}

仅关注核心问题,并简要介绍样本输入:

下面有选择地使用给定的
setName
属性值更新对象的
quantity
属性,从JSON转换为JSON:

$setName = 'Magic 2010' # the set name of interest
$newQuantity = 42       # new quantity

(@'
[
    {
        "name":  "Ponder",
        "setName":  "Commander 2018",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Lorwyn",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2010",
        "quantity":  null
    }
]
'@  | ConvertFrom-Json) | 
  ForEach-Object {
    if ($_.setName -eq $setName) {
        $_.quantity = $newQuantity
    }
    $_ # pass the (possibly updated) object through.
  } | ConvertTo-Json
请注意,需要在
(…)
中包含
ConvertFrom Json
调用,这将强制枚举从Json输入构建的数组中的各个对象(请参阅以获取背景信息)

上述收益率(注意最近更新的
数量
值):


应用于您的
updateJsonFile
函数:

function updateJsonFile($jsonfile, $setName, $newQuantity){
    $resJson = Get-Content "$jsonfile.json"
    # Note the (...) around the ConvertFrom-Json command.
    ($resJson | ConvertFrom-Json) | 
      ForEach-Object {
        if ($_.setName -eq $setName) {
          $_.quantity = $newQuantity
        }
        $_ # pass the (possibly updated) object through.
      } | ConvertTo-Json | Set-Content -Encoding Utf8 $jsonfile
}

旁白:仅使用
Format-*
cmdlet设置显示格式;如果必须以编程方式处理数据,请不要使用它们<代码>格式-*
cmdlet输出格式说明,而不是数据-请参阅。
[
  {
    "name": "Ponder",
    "setName": "Commander 2018",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Lorwyn",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Magic 2010",
    "quantity": 42
  }
]
function updateJsonFile($jsonfile, $setName, $newQuantity){
    $resJson = Get-Content "$jsonfile.json"
    # Note the (...) around the ConvertFrom-Json command.
    ($resJson | ConvertFrom-Json) | 
      ForEach-Object {
        if ($_.setName -eq $setName) {
          $_.quantity = $newQuantity
        }
        $_ # pass the (possibly updated) object through.
      } | ConvertTo-Json | Set-Content -Encoding Utf8 $jsonfile
}