I am using previous code (Convert a Log file to JSON file) to parse a log file into JSON. However, part of the fields within some groups are not available and appear in the groupings in the output as "null". How would I exclude an entire group if one of the fields contains null or incomplete group fields?
| Group sessionid | Foreach-Object {
$jsonTemplate = [pscustomobject]@{
time = [pscustomobject]@{ start = ''; duration = '' }
group1 = ''
host1 = ''
title1 = ''
address = [pscustomobject]@{from = ''; to = ''}
situation = ''
}
$start = ($_.Group | where key -eq 'title1').time
$end = ($_.Group | where key -eq 'situation').time -as [datetime]
$jsonTemplate.time.start = $begin
$jsonTemplate.time.duration = ($end - ($start -as [datetime])).ToString()
$jsonTemplate.group1 = $_.Name
$jsonTemplate.host1 = ($_.Group | where key -eq 'host1').data
$jsonTemplate.title1 = ($_.Group | where key -eq 'title1').data
$jsonTemplate.address.sender = ($_.Group | where key -eq 'sender').data
$jsonTemplate.address.reciever = ($_.Group | where key -eq 'reciever').data
$jsonTemplate.situation = ($_.Group | where key -eq 'situation').data
[regex]::Unescape(($jsonTemplate | convertTo-Json))