替换 JSON 文件中的值
Replace Values in a JSON file
我是 PowerShell 的新手。我有三个文件 output1.txt, output2.txt, deployer.json
output1.txt
db-tenant.rds.amazonaws.com
output2.txt
dev-51-db-global.rds.amazonaws.com
deployer.json
{
"user":"some_user",
"url":"some_url"
"urlglobal":"some_global_url",
"db":"some_db"
}
期望输出
{
"user":"some_user",
"url":"db-tenant.rds.amazonaws.com"
"urlglobal":"dev-51-db-global.rds.amazonaws.com",
"db":"some_db"
}
我想从 output1.txt 中获取结果并替换 url 中的值 deployer.json 文件
我也想从output2.txt中获取结果并用urlglobal[=48=中的值替换它] 在 deployer.json 文件中
这是我开始使用的脚本
脚本.ps1
$templatecontent = Get-Content -Path .\deployer.json
这很简单,希望代码是不言自明的。
$json = Get-Content path\to\deployer.json | ConvertFrom-Json
$json.url = Get-Content 'path\to\output1.txt'
$json.urlglobal = Get-Content 'path\to\output2.txt'
如果您想在不使用外部文件的情况下测试代码:
$url = 'db-tenant.rds.amazonaws.com'
$urlglobal = 'dev-51-db-global.rds.amazonaws.com'
$json = @'
{
"user":"some_user",
"url":"some_url",
"urlglobal":"some_global_url",
"db":"some_db"
}
'@ | ConvertFrom-Json
$json.url = $url
$json.urlglobal = $urlglobal
然后$json | ConvertTo-Json
输出:
{
"user": "some_user",
"url": "db-tenant.rds.amazonaws.com",
"urlglobal": "dev-51-db-global.rds.amazonaws.com",
"db": "some_db"
}
我是 PowerShell 的新手。我有三个文件 output1.txt, output2.txt, deployer.json
output1.txt
db-tenant.rds.amazonaws.com
output2.txt
dev-51-db-global.rds.amazonaws.com
deployer.json
{
"user":"some_user",
"url":"some_url"
"urlglobal":"some_global_url",
"db":"some_db"
}
期望输出
{
"user":"some_user",
"url":"db-tenant.rds.amazonaws.com"
"urlglobal":"dev-51-db-global.rds.amazonaws.com",
"db":"some_db"
}
我想从 output1.txt 中获取结果并替换 url 中的值 deployer.json 文件
我也想从output2.txt中获取结果并用urlglobal[=48=中的值替换它] 在 deployer.json 文件中
这是我开始使用的脚本
脚本.ps1
$templatecontent = Get-Content -Path .\deployer.json
这很简单,希望代码是不言自明的。
$json = Get-Content path\to\deployer.json | ConvertFrom-Json
$json.url = Get-Content 'path\to\output1.txt'
$json.urlglobal = Get-Content 'path\to\output2.txt'
如果您想在不使用外部文件的情况下测试代码:
$url = 'db-tenant.rds.amazonaws.com'
$urlglobal = 'dev-51-db-global.rds.amazonaws.com'
$json = @'
{
"user":"some_user",
"url":"some_url",
"urlglobal":"some_global_url",
"db":"some_db"
}
'@ | ConvertFrom-Json
$json.url = $url
$json.urlglobal = $urlglobal
然后$json | ConvertTo-Json
输出:
{
"user": "some_user",
"url": "db-tenant.rds.amazonaws.com",
"urlglobal": "dev-51-db-global.rds.amazonaws.com",
"db": "some_db"
}