无法使用powershell更新json文件

时间:2022-10-01 01:53:31

I am trying to update the below json file with new values and saving the file in a new path. But not able to get the desired result and my new file in the $updatedfilepath will be same as file in $jsonfilepath

我试图用新的值更新下面的json文件,并将该文件保存在新的路径中。但是无法获得所需的结果,我在$updatedfilepath中的新文件将与$jsonfilepath中的文件相同

I have tried the Set-Content powershell cmd-let with of no luck. So please can someone help me on this on guiding where i am going wrong with the code.

我尝试了集内容powershell cmd-let,但运气不好。因此,请有人能帮助我指导我的代码哪里出了问题。

My code:

我的代码:

$JsonData = Get-Content $JsonFilePath -Raw | ConvertFrom-Json
$JsonData | Where { $JsonData.modules.name -eq "qact" } | % { $JsonData.modules.load.local -replace ("qa/styles/act.css", "qa/styles/act.2.css") -replace ("qa/js/act.2.js", "qa/js/act.2.js") }
$JsonData | ConvertTo-Json -Depth 4 |  % { $_ -replace "        ","  " } | Tee-Object $updatedJsonFilePath

My File:

我的文件:

{
  "depends": "bento/1.13.0",
  "paths": [
    {
      "patterns": [ "/act/home/?.*" ],
      "use": "qact"
    },
    {
      "patterns": [ "/act/WS/home/?.*" ],
      "use": "qaws"
    }
  ],
  "modules": [
    {
      "name": "qact",
      "earlyLoad": true,
      "load": {
        "local": [  "qa/styles/act.css",
          "qa/js/act.dependencies.js",
          "qa/js/act.wijmo.xlxs.workbook.js",
          "qa/js/act.signalr.js",
          "qa/js/act.js"
        ]
      }
    },
    {
      "name": "qaws",
      "earlyLoad": true,
      "load": {
        "local": [
          "ws/styles/act.css",
          "ws/js/act.dependencies.js",
          "ws/js/act.wijmo.xlxs.workbook.js",
          "ws/js/act.signalr.js",
          "ws/js/act.js"
        ]
      }
    }
  ]
}

My Output from above code:

以上代码的输出:

qa/styles/oct.2.css
qa/js/oct.dependencies.js
qa/js/oct.wijmo.xlxs.workbook.js
qa/js/oct.signalr.js
qa/js/oct.2.js
{
  "depends": "bento/1.13.0",
  "paths": [
    {
      "patterns": [ "/act/home/?.*" ],
      "use": "qact"
    },
    {
      "patterns": [ "/act/WS/home/?.*" ],
      "use": "qaws"
    }
  ],
  "modules": [
    {
      "name": "qact",
      "earlyLoad": true,
      "load": {
        "local": [  "qa/styles/act.css",
          "qa/js/act.dependencies.js",
          "qa/js/act.wijmo.xlxs.workbook.js",
          "qa/js/act.signalr.js",
          "qa/js/act.js"
        ]
      }
    },
    {
      "name": "qaws",
      "earlyLoad": true,
      "load": {
        "local": [
          "ws/styles/act.css",
          "ws/js/act.dependencies.js",
          "ws/js/act.wijmo.xlxs.workbook.js",
          "ws/js/act.signalr.js",
          "ws/js/act.js"
        ]
      }
    }
  ]
}

My Desired Result:

我想要的结果:

{
  "depends": "bento/1.13.0",
  "paths": [
    {
      "patterns": [ "/act/home/?.*" ],
      "use": "qact"
    },
    {
      "patterns": [ "/act/WS/home/?.*" ],
      "use": "qaws"
    }
  ],
  "modules": [
    {
      "name": "qact",
      "earlyLoad": true,
      "load": {
        "local": [  "qa/styles/act.2.css",
          "qa/js/act.dependencies.js",
          "qa/js/act.wijmo.xlxs.workbook.js",
          "qa/js/act.signalr.js",
          "qa/js/act.2.js"
        ]
      }
    },
    {
      "name": "qaws",
      "earlyLoad": true,
      "load": {
        "local": [
          "ws/styles/act.css",
          "ws/js/act.dependencies.js",
          "ws/js/act.wijmo.xlxs.workbook.js",
          "ws/js/act.signalr.js",
          "ws/js/act.js"
        ]
      }
    }
  ]
}

1 个解决方案

#1


0  

I assume it's fine to do it via a simple string replace ...

我认为可以通过一个简单的字符串替换……

$jsonFilePath = "c:\folder\input.json"         #replace with path to your input file
$updatedJsonFilePath = "c:\folder\output.json" #replace with path to your output file

$jsonData = Get-Content $jsonFilePath 
$jsonData -ireplace ("styles/act.css", "styles/act.2.css") -ireplace ("js/act.js", "js/act.2.js") | Out-File $updatedJsonFilePath

#1


0  

I assume it's fine to do it via a simple string replace ...

我认为可以通过一个简单的字符串替换……

$jsonFilePath = "c:\folder\input.json"         #replace with path to your input file
$updatedJsonFilePath = "c:\folder\output.json" #replace with path to your output file

$jsonData = Get-Content $jsonFilePath 
$jsonData -ireplace ("styles/act.css", "styles/act.2.css") -ireplace ("js/act.js", "js/act.2.js") | Out-File $updatedJsonFilePath