簡體   English   中英

如何使用powershell更改長JSON值中的url?

[英]How to change url in long JSON value using powershell?

我有一個 JSON 文件:

"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }

我需要使用 powershell 更改 --feurl --beurl --feadminurl --db --env 。

我嘗試使用該函數,但它更改了完整值:

 function Update-JsonParameter($directory, $jsonFile, $property, $subproperty, $value)
{
try
{
write-host "Update $property.$subproperty property in JSON file $directory\$jsonFile"
$jsonFile = "$directory\$jsonFile"
$convertJson = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json
$convertJson.$property.$subproperty = "$value"
$convertJson | ConvertTo-Json | set-content $jsonFile
}
catch
{
write-host "Updating JSON file FAILED!"
throw $Error[0].Exception
}
}

我該如何實施?

如果沒有函數,您可以使用哈希表來管理要更改的參數。 然后只需對每個參數使用-replace 您可以使用GetEnumerator()方法遍歷哈希表。

$json = @'
{
"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }
}
'@
$ArgsToChange = @{ feurl = 'https://newfeurl.com'; beurl = 'https://newbeurl.com'; feadminurl = 'https://newfeadminurl.com'; db = 'newdb'; env = 'newenv'}
$jo = $json | ConvertFrom-Json

foreach ($arg in $ArgsToChange.GetEnumerator()) {
    $jo.scripts.'test:ui:start-local-x-run' = $jo.scripts.'test:ui:start-local-x-run' -replace "(?<=--$($arg.Key) ).*?(?=\s--|$)",$arg.Value
}

$updatedJSON = $jo | ConvertTo-Json

如果您始終知道傳遞目標腳本的所有參數,那么您仍然可以使用哈希表來跟蹤參數,但您可以使用其數據構建參數字符串。

$json = @'
{
"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }
}
'@

# Hash table of arguments and values (syntax: --argument value)
$ArgsToChange = [ordered]@{ disableChecks = $null; feurl = 'https://newfeurl.com'; beurl = 'https://newbeurl.com'; feadminurl = 'https://newfeadminurl.com'; db = 'newdb'; env = 'newenv'}
$jo = $json | ConvertFrom-Json

# Iterating over the hash table creating string of arguments
# Expected output string of --argument1 value1 --argument2 value2 for example
$ArgString = ($ArgsToChange.GetEnumerator() |% { "--{0} {1}" -f $_.Key,$_.Value }).Trim() -join ' '

# Grab string before the first --argument
$program = ($jo.scripts.'test:ui:start-local-x-run' -split '--')[0]

# Join program string and new argument string
$jo.scripts.'test:ui:start-local-x-run' = $program + $ArgString

$updatedJSON = $jo | ConvertTo-Json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM