1

I have a myJson.json that look like this:

{
  "FirewallGroupsToEnable": [
    "Remote Event Log Management",
    "Windows Remote Management",
    "Performance Logs and Alerts",
    "File and Printer Sharing",
    "Windows Management Instrumentation (WMI)"
  ],
  "MemoryStartupBytes": "3GB"
}

I'd like to serialize it as a string and then set it as a variable to be used by other tasks. If there is a better way to use this file inside a pipeline please let me know.

I'm serializing it and setting it like this:

          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |

                  $Configs= Get-Content -Path $(Build.SourcesDirectory)\sources\myJson.json -Raw | ConvertFrom-Json
                  
                  Write-Host "##vso[task.setvariable variable=Configs]$Configs"

In the following task, I am running a PowerShell script.

          - task: PowerShell@2
            displayName: 'myTask'
            inputs:
              targetType: 'filePath'
              filePath: 'sources\myScript.ps1'
              pwsh: true

I'm using the variable in my script like this:

$env:Configs
[Configs]$envConfigs = ConvertFrom-Json -InputObject $env:Configs -ErrorAction Stop

The Configs is a class that is being imported at the top of the script like so Using module .\Configs.psm1. I know it's being read because if it wasn't the error would be about a missing type.

Configs.psm1

class Configs
{
    [string[]]$FirewallGroupsToEnable
    [string]$MemoryStartupBytes
}

This is what I get in the pipeline.

##[debug]Processed: ##vso[task.setvariable variable=Configs]@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}

@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}
Cannot convert the "@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}" value of type "System.String" to type "Configs".

I've always casted a deserialized JSON into custom types like this and it always worked. But right now there is something wrong!

I tried to remove ConvertFrom-Json while serializing the JSON (before setting it as a variable) but it doesn't serialize it right. It shows like this in the pipeline:

enter image description here

It looks like it's only getting the first curly braces!

So, how do I serialize a JSON regardless of its depth into the pipeline to be used in later tasks inside a script file?

1
  • Hi @jsnoobie, is the answer below to pass JSON data via a file helps you? If it helps, just a remind of accept an answer. Commented Nov 25, 2022 at 1:23

2 Answers 2

1

You could pass the json data via a file, here is my YAML snippet that can pass the full JSON content successfully in next task.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $Configs= Get-Content -Path $(Build.SourcesDirectory)\wit.json
      $Configs | Out-File Test.json

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $Input = Get-Content Test.json
      Write-Host $Input

Here are similar ticket about pass Json variable for reference.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not really the answer. But close. My solution is correct. However, I can't Write-Host multi-line variables even if they were strings. Hence, I had to ConvertFrom-Json and then ConvertTo-Json -Compress to get my string in one line.
1

As it turns out, you can't Write-Host multi-line variables even if they were strings due to the fact of PowerShell formatting. Hence, I had to ConvertFrom-Json and then ConvertTo-Json -Compress to get my string in one line.

$Configs= Get-Content -Path $(Build.SourcesDirectory)\sources\myJson.json -Raw | ConvertFrom-Json | ConvertTo-Json -Compress

Comments

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.