简体   繁体   中英

Amending Existing JSON Files in Powershell 4

I have seen many questions around this however none of the answers seem to work (please correct me if i am wrong)

I have created a function that queries an API and saves the result as a json file.
However I would like to amend the file that is saved

powershell code:

$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 generated (searching for the card 'Ponder')

[
    {
        "name":  "Ponder",
        "setName":  "Commander 2018",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Lorwyn",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2010",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic 2012",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic Player Rewards 2008",
        "quantity":  null
    },
    {
        "name":  "Ponder",
        "setName":  "Magic Online Promos",
        "quantity":  null
    }
]

What I would like to do is load the file and amend the "quantity" for a particular set.
Can anyone point me in the right direction?

  • Update -

I will get the json content from a file:

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

Focusing just on the core question, with abridged sample input:

The following selectively updates the quantity property for an object with a given setName property value, converting from and to 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

Note the need to enclose the ConvertFrom-Json call in (...) , which forces enumeration of the individual objects in the array constructed from the JSON input (see this answer for background information).

The above yields (note the updated last quantity value):

[
  {
    "name": "Ponder",
    "setName": "Commander 2018",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Lorwyn",
    "quantity": null
  },
  {
    "name": "Ponder",
    "setName": "Magic 2010",
    "quantity": 42
  }
]

Applied to your updateJsonFile function:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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