How do I update JSON file using PowerShell

前端 未结 1 1160
星月不相逢
星月不相逢 2020-12-13 08:59

I have one json file mytest.json like below I want to update values using PowerShell script

update.json

{
    \"update\": [         


        
相关标签:
1条回答
  • 2020-12-13 09:38

    Here is a way :

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

    According to @FLGMwt and @mikemaccana I improve the ConvertTo-Json with -depth 32 because the default depth value is 2 and for object deeper than 2 you will receive class informations in spite of objects.

    0 讨论(0)
提交回复
热议问题