0

I have the below json file

{
    "name": "ca",
    "version": "5.2.0"
}

I am extracting the value of version and trying to assign it to a variable ver

$jsonString = Get-Content -Path ./package.json
$jsonObj = $jsonString | ConvertFrom-Json

echo $jsonObj.version

Write-Host "##vso[task.setvariable variable=ver]$jsonObj.version"

echo $ver

Below is the output

5.2.0

I am expecting the value to be printed twice and also get it assigned to the variable ver but it is not getting assigned

3
  • 1
    Nope, in PowerShell, you've assigned it once and printing it once. You haven't provided detail as to what you're trying to do, i.e. what is the vso stuff? Commented Apr 22, 2022 at 6:24
  • @codaamok I am trying to assign it to a variable so that I can use it in the output of the azure devops pipeline. So, I got vso from Microsoft Documents of Azure Devops Pipeline Commented Apr 22, 2022 at 8:28
  • Yeah, sounds like that's not the same as a PowerShell variable. Perhaps a variable in the ADO engine/runner instead? Commented Apr 22, 2022 at 8:30

1 Answer 1

1

If you are trying to use it in builds, you will not see a new value in the same step. Additionally, use $($jsonObj.version) in the logging command:

steps:
- powershell: |
   
   $jsonString = Get-Content -Path ./package.json
   $jsonObj = $jsonString | ConvertFrom-Json
   
   $jsonObj.version
   
   Write-Host "##vso[task.setvariable variable=my.ver]$($jsonObj.version)"

  displayName: 'Set vars'

- powershell: |
   $ver = '$(my.ver)'
   
   $ver
   
  displayName: 'Read vars'
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to set Write-Host "##vso[task.setvariable variable=VAR]$($jsonObj)" and in second task get nested value '$(VAR.version)' ?
@itiic I do not think so. However, you can try to convert your object to json-string without new lines (like, ConvertTo-Json -Compress). Then in the next steps convert it to an object.
Yes, minified JSON is being passed. Example: task1: $minifyJSON = Get-Content 'variables.json' | ConvertFrom-Json | ConvertTo-Json -Compress Write-Host "##vso[task.setvariable variable=VARS;]$minifyJSON" task2: $V = '$(VARS)' | ConvertFrom-Json Write-Host "$($V.key.key_nested)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.