2016-03-08 12 views
12

Ho un file JSON mytest.json come qui di seguito voglio aggiornare i valori utilizzando powershell scriptcome faccio a aggiornare il file JSON utilizzando PowerShell

update.json

{ 
    "update": [ 
     { 
      "Name": "test1",   
      "Version": "2.1" 
     }, 
     { 
      "Name": "test2",   
      "Version": "2.1" 
     } 
    ] 
} 

Voglio scrivere uno script PowerShell in cui if Name=="test1" I want to update Version= "3" come posso farlo usando i parametri?

risposta

23

Ecco un modo:

$a = Get-Content 'D:\temp\mytest.json' -raw | ConvertFrom-Json 
$a.update | % {if($_.name -eq 'test1'){$_.version=3.0}} 
$a | ConvertTo-Json | set-content 'D:\temp\mytestBis.json' 
+0

opere super veloce grande grazie @JPBlanc – Neo

+15

articoli che 'ConvertTo-Json' ha una profondità di default 2. JSON profonda andranno' ToString''d che è probabilmente non quello che vuoi Se si dispone di più nested json, utilizzare il parametro 'Depth':' ConvertTo-Json -Depth 20' – FLGMwt

Problemi correlati