I have a JSON file :
"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"
}
I need to change --feurl --beurl --feadminurl --db --env using powershell.
I've tried to use the function but it changes full value:
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
}
}
How can I implement it?
Without a function, you could use a hash table to manage the arguments you want to change. Then just use -replace
for each of those arguments. You can iterate over a hash table with GetEnumerator()
method.
$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
If you always know all of the arguments to pass the target script, then you can still use a hash table to track the arguments, but you can instead build your argument string with its data.
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.