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
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.countor$json.tactics[0].$jsona string containing the json?('{"displayName":"travel with mailbox permission","tactics":["InitialAccess","PrivilegeEscalation"],"techniques":["T1078","T1548"]}' |ConvertFrom-Json).tactics.Count