Author Posts

January 26, 2018 at 10:19 am

I have JSON data which I would like to process with Powershell. I can select separate nodes and work with that, but I do not know how can I join several nodes (in case of a goal, I have id_goal which holds ID of a player name from roster (Team1 or Team2). Can some guru take a look and help me?

I am able to pull out as a Out-Gridview a list of goals and with -PassTrough I can work with selected data. But I am missing Player Name (I just pull his ID). How would you join this "tables"? This is the JSON data: http://textuploader.com/dmwbf

this is the half working code:

$MatchInfo = Invoke-WebRequest $request | ConvertFrom-Json

$Competition_id = $MatchInfo.competition.id_competition

$Goals=$MatchInfo.goals
$RosterA=$MatchInfo.team1
$RosterB=$MatchInfo.team2

$GoalsTable = @()
$A=0
$B=0
foreach ($record in $Goals)
{
$GoalsRecord = @{}
$GoalsRecord.id_goal=$record.id_goal
$GoalsRecord.id_match=$record.id_match
$GoalsRecord.id_team=$record.id_team
$GoalsRecord.time=$record.time
$GoalsRecord.goal=$record.goal
$GoalsRecord.assist1=$record.assist1
$GoalsRecord.assist2=$record.assist2
$GoalsRecord.situation=$record.situation
$GoalsRecord.pos1=$record.pos1
$GoalsRecord.pos2=$record.pos2
$GoalsRecord.pos3=$record.pos3
$GoalsRecord.pos4=$record.pos4
$GoalsRecord.pos5=$record.pos5
$GoalsRecord.pos6=$record.pos6
$GoalsRecord.neg1=$record.neg1
$GoalsRecord.neg2=$record.neg2
$GoalsRecord.neg3=$record.neg3
$GoalsRecord.neg4=$record.neg4
$GoalsRecord.neg5=$record.neg5
$GoalsRecord.neg6=$record.neg6
if($record.id_team -eq $MatchInfo.team1.id_team){
$A=$A+1
$PlayerName=$MatchInfo.team1.roster | select first_name, last_name | where {$MatchInfo.team1.roster.id_player -eq $record.goal }
$GoalsRecord.PlayerName = $PlayerName
}

if($record.id_team -eq $MatchInfo.team2.id_team){
$B=$B+1
$PlayerName=$MatchInfo.team1.roster | select first_name, last_name | where {$record.goal -eq $MatchInfo.team1.roster.id_player }
$GoalsRecord.PlayerName = $PlayerName
}

$GoalsRecord.score=$A.ToString()+":"+$B.ToString()

$GoalsTable += ([PSCustomObject]$GoalsRecord)

}

$GoalsTable | Sort score -Descending | Out-GridView -PassThru

January 26, 2018 at 5:58 pm

Can you post sample data for $MatchInfo after line 1?