Home > database >  Powershell - JSON string with variables as parameter
Powershell - JSON string with variables as parameter

Time:10-26

I am trying to use Databricks CLI in PowerShell. I need to pass JSON string as parameter.

I have two variables - job_id equals to 10 and parameterValue equal to some string.

I used like 4 different combinations, but still getting an error of Error: JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Code used:

databricks jobs run-now --job-id $job_id --notebook-params "{""parameterName"":""$parameterValue""}"
databricks jobs run-now --job-id $job_id --notebook-params "{`"parameterName`":`"$parameterValue`"}"
databricks jobs run-now --job-id $job_id --notebook-params '{"parameterName":"$parameterValue"}'

CodePudding user response:

Ok, so what I did is

  1. Write python code to print passed argument
if __name__ == '__main__':
    import sys
    print(sys.argv[1])

2. Tested this with different setups and for example:

input: $s="{`"parameterName`":`"$parameterValue`"}"

powershell output: {"tableName":"some_string"}

python output: {parameterName:some_string}

So python completely ignored the double quotes, even they were escaped.

I had to:

  1. Escape ( \ ) the escape ( ` ) character:

powershell input: $s="{\`"parameterName\`":\`"$parameterValue\`"}"

powershell output: {\"parameterName\":\"some_string\"}

python output: {"parameterName":"some_string"}



The only problem I have not figured out yet is when $parameterValue variable includes space, then it does not work:

powershell input:

$parameterValue='some string'
$s="{\`"parameterName\`":\`"$parameterValue\`"}"

powershell output: {\"parameterName\":\"some string\"}

python output: {"parameterName":"some

CodePudding user response:

It is a bad practice to try to build your own serialized strings, instead use the intended methods and cmdlets (such as ConvertTo-Json):

$job_id = 10
$parameterName = 'Some String'
$Data = @{
    job_id = $job_id
    parameterName = $parameterName
}
$Json = ConvertTo-Json $Data
databricks jobs run-now --job-id $job_id --notebook-params $Json

or compressed:

databricks jobs run-now --job-id $job_id --notebook-params (ConvertTo-Json -Compress @{ parameterName = $parameterName })
  • Related