The values are passed to next step , after when I convertfrom -json getting empty varaibles

step1:
setting up the values to variable,
Set-OctopusVariable -Name “create_values” -Value ($create_appids | convertto-json)
step2:
able to read the values in step 2
$create_obj = $OctopusParameters[“Octopus.Action[Generate Report].Output.create_values”]
write-host “create_values_json:” $create_obj — > This step print the all the data
$create_obj_fromjson = $create_obj | convertfrom-json
write-host “create_values_fromjson:” $create_obj_fromjson after convertfrom-json – it returns empty values

Hi @YuvarajDuraisamy

Thanks for contacting us regarding your issue.
I have set up a similar scenario in my test environment but cant reproduce the issue you are facing below is what I have set up:

Step 1 (Generate Report):

$JSONData = @{
	"Name" = "Dom"
}

Set-OctopusVariable -name "create_values" -value ($JSONData | Convertto-Json -depth 20)

Step 2 (Display Report):

 $JSONStr = $OctopusParameters["Octopus.Action[Generate Report].Output.create_values"]
 echo $JSONStr

 $JSONData = $JSONStr | convertfrom-json
 echo $JSONData

Would you be able to try the above script to see if you are able to get the same result as I did?

Would you also be able to send us the Raw Task log using our Secure Upload Link?

Kind Regards,
Dom.

The above code is working fine , in my case the number dictionary values adding to the array then convertto -json passing to the next step
the same value able to read ,but when converting it ,it returns empty .

the same approach works fine in my local
I have attached raw logs and the file which generated with json data

Hi @YuvarajDuraisamy,

Thanks for getting back to us and sorry to hear that still isn’t working for you.

I’m not quite sure how you’re building the Powershell object before it gets converted to JSON, but I was able to get your example working using separate objects and combining them before converting to JSON. My example objects looked like the following:

$JSONData = @{
    Certificate= "Certificate1"
    SiteName= "Site1"
    Environment= "Environment1"
}

$JSONData2 = @{
    Certificate= "Certificate2"
    SiteName= "Site2"
    Environment= "Environment2"
}

I then combined these when creating the JSON object:

Set-OctopusVariable -name "create_values" -value (ConvertTo-Json @($JSONData, $JSONData2))

Then, in the second step, I’ve basically followed what Dom wrote above and it outputs the object after converting from JSON:

 $JSONStr = $OctopusParameters["Octopus.Action[Generate Report].Output.create_values"]
 echo $JSONStr

 $JSONData = $JSONStr | convertfrom-json
 echo $JSONData[0]

Note the $JSONData[0] is just to access the first element in the Powershell object, and the output looks like the following:

Does that work on your end, and are you able to adapt it to your process?

Let me know what you find!

Best,
Patrick

I did the same way , like declared an array and adding the dictionary values to array then convert to json ,

$create_appids = @()
$application_details = @{

    EnvironmentName   = $env_name
    LabsiteName       = $lab_name
    ApplicationName   = $app_names
    ComponentName     = $component_name
    AppIdentifierName = $app_identifier
    CertificateName   = $certificate_name
    ServerNames       = $server_names
    VariableName      = $variable_name
    Status            = $status
    Permissions       = $permissions

}

$create_appids += $application_details —> we can do like this right ?
Set-OctopusVariable -Name “create_appid_values” -Value ($create_appids | convertto-json)

in the second step ,
$JSONStr = $OctopusParameters[“Octopus.Action[Generate Report].Output.create_values”]

write-host “JSONStr:” $JSONStr → able to read the json string values
$JSONData = $JSONStr | convertfrom-json
write-host “JSONData:” $JSONData → after converting it returns empty value

Hi @YuvarajDuraisamy
In the second step would you be able to confirm that the $JSONStr is valid json using a JSON linter?

Would you also try and add the -depth 20 parameter to the ConvertTo-Json commandlet to see if that resolves the issue?

Kind Regards,

Dom.

its valid json string and its working fine.
when I try to print $JSONData it returns empty ,
so I have like below and works fine
$JSONStr = $OctopusParameters[“Octopus.Action[Generate Report].Output.delete_values”]

#write-host “JSONStr:” $JSONStr
$JSONData = $JSONStr | convertfrom-json

foreach($val in $JSONData)
{
write-host $val.LabsiteName
write-host $val.ApplicationName
write-host $val.ComponentName
write-host $val.AppIdentifierName
write-host $val.CertificateName
write-host $val.ServerNames
write-host $val.VariableName
write-host $val.Status
write-host $val.Permissions
}

Thanks for your support

1 Like

Hi @YuvarajDuraisamy,

Thanks for getting back to us with those details.

Just to confirm, when you printed out the $JSONdata via the foreach loop, did that give you the output you’re expecting? I’ve tested what you’ve provided so far and it looks like it’s working on my end, so I believe that’s the case. If not, could you provide a few more details on what part of your process isn’t working?

I’m looking forward to hearing back from you.

Best,
Patrick

yes, when I printed out $json data via foreach loop, it works fine

Hi @YuvarajDuraisamy

Thanks for confirming that for us.
When printing out the $JSONData after the convertfrom-json would you be able to to try this:

write-host $JSONData

Instead of: write-host "JSONData:" $JSONData

And see if that printing out the data correctly?

I feel the issue might lie when trying to convert the PowerShell object to a string when writing to host.

If that does work please could you send us the JSON that was printed before converting to a PowerShell object?

You can send it to our secure upload site here.

Kind Regards,
Dom.