-1

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
6
  • Please edit your question and create a valid minimal reproducible example. In its current state it produces a (Unexpected token ':' in expression or statement.) error. With regards to the issue, this is just how it is displayed, the actual object is probably just an array. Try $json.tactics.GetType(), $json.tactics.count or $json.tactics[0]. Commented Feb 7, 2023 at 10:51
  • The sample code you've posted is not valid PowerShell. Is $json a string containing the json? Commented Feb 7, 2023 at 11:10
  • @iRon $json.tactics.count is coming as 1 eventhough it has 2 values Commented Feb 7, 2023 at 11:14
  • 1
    Edited my code in the above post Commented Feb 7, 2023 at 11:24
  • 1
    "$json.tactics.count is coming as 1 eventhough it has 2 value". For me it returns 2: ('{"displayName":"travel with mailbox permission","tactics":["InitialAccess","PrivilegeEscalation"],"techniques":["T1078","T1548"]}' |ConvertFrom-Json).tactics.Count Commented Feb 7, 2023 at 11:55

1 Answer 1

1

Try using

$json.tactics -join ','

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

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.