0

I have some JSON like this (which came from a PSObject):

[
    {
        "cf_workflow_1":  1212,
        "cf_workflow_2":  null,
        "cf_workflow_3":  null
    },
    {
        "cf_workflow_1":  null,
        "cf_workflow_2":  9797,
        "cf_workflow_3":  null
    },
    {
        "cf_workflow_1":  6262,
        "cf_workflow_2":  null,
        "cf_workflow_3":  null
    }
]

I want to pull out an array that is simply:

(1212,9797,6262)

How would I go about doing that?

Here is some sample code to work with:

$tickets = @"
        [
            {
                "cf_workflow_1":  1212,
                "cf_workflow_2":  null,
                "cf_workflow_3":  null
            },
            {
                "cf_workflow_1":  null,
                "cf_workflow_2":  9797,
                "cf_workflow_3":  null
            },
            {
                "cf_workflow_1":  6262,
                "cf_workflow_2":  null,
                "cf_workflow_3":  null
            }
        ]
"@

$ticketsObject = $tickets | ConvertFrom-Json 

#Show the ticket data
$ticketsObject | Format-Table -AutoSize

#Create an array to hold all the non-null workflow numbers
#It should eventually contain the value (1212,9797,6262)
$workflowNumbers = @()

#Not sure what to do here to return only the non-null workflow numbers as an array?
$workflowNumbers = $ticketsObject | foreach{$_}
1

1 Answer 1

0

This works:

$ticketsObject | 
  ForEach-Object {$_.Psobject.properties | 
                 where-Object {$_.Name -like 'cf*' -and $_.Value } | select -expand Value}

The idea was to look at the properties of each object (only the cf* ones) and only the ones that had a value, and then just get the Values.

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

Comments

Your Answer

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