简体   繁体   English

如何使用PowerShell替换具有变量的JSON文件中的字符串

[英]How to replace string in json file which have a variable using powershell

I have a json as below. 我有一个JSON如下。 Want to replace '$content' in the json with 'test/abc' 想要用'test / abc'替换json中的'$ content'

{
"$version": "0.0.1",
"description": "",
"content": {
    "url": "$content",
    "tag": {
        "version": "0.0.1",

    },
    "buildOptions": []
},
"env": "qa"}

I tried by converting it to JSON object but since there is a variable in that in gives me "ConvertFrom-Json : Invalid JSON primitive" error. 我尝试将其转换为JSON对象,但是由于其中存在一个变量,因此出现“ ConvertFrom-Json:无效的JSON原语”错误。

 $JsonData = Get-Content module.json -raw | ConvertFrom-Json

How can I replace $content with test/abc? 如何用test / abc替换$ content?

you can escape the $ - either manually or with the [regex]::Escape() method. 您可以手动或使用[regex]::Escape()方法对$进行[regex]::Escape() [ grin ] [ 咧嘴 ]

if $InStuff contains your sample data in a here-string, then this will do the replacement ... 如果$InStuff在here字符串中包含示例数据,则将执行替换操作...

$InStuff -replace [regex]::Escape('$content'), 'test/abc'

hope that helps, 希望能有所帮助,
lee 背风处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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