无法使用powershell更新json文件

时间:2018-06-01 14:10:55

标签: json windows powershell scripting

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

我试过Set-Content powershell cmd-let没有运气。所以,有人可以帮助我指导我在代码中出错的地方。

我的代码:

$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

我的档案:

{
  "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"
        ]
      }
    }
  ]
}

以上代码的输出

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"
        ]
      }
    }
  ]
}

我想要的结果:

{
  "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 个答案:

答案 0 :(得分:1)

我认为可以通过简单的字符串替换来实现它...

$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