dev333
dev333

Reputation: 799

How to retrieve JSON data using PowerShell?

I have this JSON. Trying to retrieve tactics array by using below code in PowerShell.

Why the actual output is weird even though tactics contains comma separated values, it is considering as one value with space separated values.

How to get the expected output?

My JSON:

  {
       "displayName": "travel with mailbox permission",
     "tactics": [
            "InitialAccess",
            "PrivilegeEscalation"
          ],
          "techniques": [
            "T1078",
            "T1548"
          ]
  }

My code:

param (
    [Parameter(Mandatory = $true)]
     # The resourceVariable which holds the resource details
    [string] $jsonvariable
    
)   
 $json = Get-AutomationVariable -Name $jsonvariable
 $jsonObject = ConvertFrom-Json -InputObject $json
            
         echo $jsonObject.tactics

Output:

 Expected o/p:
 InitialAccess,PrivilegeEscalation
            
 Actual O/p :
 InitialAccess PrivilegeEscalation

Upvotes: -1

Views: 68

Answers (1)

Anthony Norwood
Anthony Norwood

Reputation: 442

Try using

$json.tactics -join ','

Powershell is outputting each value in the array separately, rather than as a comma-separated list.

Upvotes: 1

Related Questions