Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

Replace values in Json using powershell

I have a json file in which i would like to change values and save again as a Json: Values that need to be updated:

  1. domain

  2. repo

    [
     {
           "name": "[concat(parameters('factoryName'), '/LS_New')]",
           "type": "Microsoft.DataFactory/factories/linkedServices",
           "apiVersion": "2018-06-01",
           "properties": {
             "description": "Connection",
             "annotations": [],
             "type": "AzureDatabricks",
             "typeProperties": {
               "domain": "https://url.net",
               "accessToken": {
                 "type": "AzureKeyVaultSecret",
                 "store": {
                   "referenceName": "LS_vault",
                   "type": "LinkedServiceReference"
                 },
                 "secretName": "TOKEN"
               },
               "newClusterNodeType": "Standard_DS4_v2",
               "newClusterNumOfWorker": "2:10",
               "newClusterSparkEnvVars": {
                 "PYSPARK_PYTHON": "/databricks/python3/bin/python3"
               },
               "newClusterVersion": "7.2.x-scala2.12"
             }
           },
           "dependsOn": [
             "[concat(variables('factoryId'), '/linkedServices/LS_evaKeyVault')]"
           ]
         },
     {
           "name": "[concat(parameters('factoryName'), '/PIP_Log')]",
           "type": "Microsoft.DataFactory/factories/pipelines",
           "apiVersion": "2018-06-01",
           "properties": {
             "description": "Unzip",
             "activities": [
               {
                 "name": "Parse",
                 "description": "This notebook",
                 "type": "DatabricksNotebook",
                 "dependsOn": [],
                 "policy": {
                   "timeout": "7.00:00:00",
                   "retry": 0,
                   "retryIntervalInSeconds": 30,
                   "secureOutput": false,
                   "secureInput": false
                 },
                 "userProperties": [],
                 "typeProperties": {
                   "notebookPath": "/dataPipelines/main_notebook.py",
                   "baseParameters": {
                     "businessgroup": {
                       "value": "@pipeline().parameters.businessgroup",
                       "type": "Expression"
                     },
                     "project": {
                       "value": "@pipeline().parameters.project",
                       "type": "Expression"
                     }
                   },
                   "libraries": [
                     {
                       "pypi": {
                         "package": "cytoolz"
                       }
                     },
                     {
                       "pypi": {
                         "package": "log",
                         "repo": "https://b73gxyht"
                       }
                     }
                   ]
                 },
                 "linkedServiceName": {
                   "referenceName": "LS_o",
                   "type": "LinkedServiceReference"
                 }
               }
             ],
             "parameters": {
               "businessgroup": {
                 "type": "string",
                 "defaultValue": "test"
               },
               "project": {
                 "type": "string",
                 "defaultValue": "log-analytics"
               }
             },
             "annotations": []
           },
           "dependsOn": [
             "[concat(variables('factoryId'), '/linkedServices/LS_o')]"
           ]
         }
     ]
    

I tried using regex but i am only able to update 1 value :

<valuesToReplace>
  <valueToReplace>
    <regExSearch>(/PIP_Log[wW]*?[pP]roperties[wW]*?[lL]ibraries[wW]*?[pP]ypi[wW]*?"repo":s)"(.*?[^\])"</regExSearch>
    <replaceWith>__PATValue__</replaceWith>
  </valueToReplace>

  <valueToReplace>
    <regExSearch>('/LS_New[wW]*?[pP]roperties[wW]*?[tT]ypeProperties[wW]*?"domain":s"(.*?[^\])")</regExSearch>
    <replaceWith>__LSDomainName__</replaceWith>
  </valueToReplace>
</valuesToReplace>

Here is the powershell code. The loop goes through all the values that are to be replaced. I tried using dynamic variable in select-string and looping, but it doesn't seem to work

foreach($valueToReplace in $configFile.valuesToReplace.valueToReplace)
        {
          
            $regEx = $valueToReplace.regExSearch
            $replaceValue = '"' + $valueToReplace.replaceWith + '"'
          
            $matches = [regex]::Matches($json, $regEx)
            
            $matchExactValueRegex = $matches.Value | Select-String -Pattern """repoD:s*(.*)" | % {$_.Matches.Groups[1].Value}
            $updateReplaceValue = $matches.Value | Select-String -Pattern "repoD:sD__(.*)__""" | % {$_.Matches.Groups[1].Value}
            $updateReplaceValue = """$patValue"""
    
    $json1 = [regex]::Replace($json, $matchExactValueRegex , $updateReplaceValue)
    
    $matchExactValueRegex1 = $matches.Value | Select-String -Pattern """domainD:s*(.*)" | % {$_.Matches.Groups[1].Value}
            $updateReplaceValue1 = $matches.Value | Select-String -Pattern "domainD:sD__(.*)__""" | % {$_.Matches.Groups[1].Value}
            $updateReplaceValue1 = """$domainURL"""
     
          
            $json = [regex]::Replace($json1, $matchExactValueRegex1 , $updateReplaceValue1)
          }
          else
          {
            Write-Warning "Inactive config value"
          }
        
        
        $json | Out-File $armFileWithReplacedValues

Where am i missing??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not peek and poke in serialized files (as e.g. Json files) directly. Instead deserialize the file with the ConvertFrom-Json cmdlet, make your changes to the object and serialize it again with the ConvertTo-Json cmdlet:

$Data = ConvertFrom-Json $Json
$Data[0].properties.typeproperties.domain = '_LSDomainName__'
$Data[1].properties.activities.typeproperties.libraries[1].pypi.repo = '__PATValue__'
$Data | ConvertTo-Json -Depth 9 | Out-File $armFileWithReplacedValues

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...