Good morning. Stack Overflow, Powershell, RegEx novice here.
I am interested in making an enhancement to the following program in Powershell. As of now, the program searches for an entityType of "IHUR" inside of a ForEach. Once "IHUR" is found, the program yields two objects of interest: id and RatedOI.
I have a complex JSON file where id and RatedOI and IHUR are appearing multiple times in the JSON file. I just realized IHUR only occurs once in the JSON. Since the current program is only returning the 1st occurrence of id and RatedOI, I'm looking to enhance it.
When entityType "Operator" is found, capture only the first ID occurrence, physically located 1 line above it (upward scan?)
When entityType "Operator" is found, capture only the first RatedOperatorIndicator below it (downward scan?) (RatedOI can be found 1 line below or many lines below)
If the following string is found (which are on separate lines in the JSON, then yield no results:
"EntityType": "Operator",
"Characteristics": [
Please Notice the bracket, not the curly brace
id RatedIO
---- ------
833e2a7f-c173-4062-9cd6-2196432e0001 False
6fe06340-072f-49b2-8d5a-449e3db80002 True
Id RatedOI
---- ------
833e2a7f-c173-4062-9cd6-2196432e0001 False
Can someone please review this sample JSON file and PowerShell code and provide feedback?
JSON code
{
"id": "4fd338f7-746b-420b-9d7e-b41709e9a641",
"RD": {
"PDR": []
},
"TimeStamp": "2025-02-14T16:55:20.1433467Z",
"NNTl": {
"CD": [
{
"EntityType": "Occurrence",
"Characteristics": [
"OFGE",
"ORI"
],
"Cardinality": "Many"
},
{
"EntityType": "Operator",
"Characteristics": [
"ADL",
"ERO"
],
"Cardinality": "Many"
}
],
"RRXC": true
},
"EntitiesData": {
"Entities": [
{
"Id": "b0000000-0000-0000-0000-00000000000b",
"EntityType": "IHUR",
"Ordinal": 0,
"Characteristics": {
"PBIL": {
"StringValue": "DDDD"
}
}
},
{
"Id": "833e2a7f-c173-4062-9cd6-2196432e0001",
"EntityType": "Operator",
"Ordinal": 0,
"Characteristics": {
"FN": {
"StringValue": "Da"
},
"RatedOI": {
"BoolValue": false
},
"fdfdf": {
"DDFDF": "SDFDS43R5"
}
},
"EntityCreationDateTime": "2025-02-12T19:42:59.2852792Z"
},
{
"Id": "6fe06340-072f-49b2-8d5a-449e3db80002",
"EntityType": "Operator",
"Characteristics": {
"Deleted": {
"StringValue": "0"
},
"RatedOI": {
"BoolValue": true
}
}
}
]
},
"OR": "FFFFF01"
}
Powershell code, based on this answer:
Set-Location C:\Users\A187515\Downloads\
$file = Get-ChildItem -Path C:\Users\A187515\Downloads\ -Filter *.json | Sort-Object LastAccessTime -Descending | Select-Object -First 1
(Get-Content -Raw "$file" | ConvertFrom-Json) | ForEach-Object {
$useNext = $false
foreach ($entity in $_.entitiesData.entities) {
if ($entity.entityType -eq 'IHUR') {
$useNext = $true
}
elseif ($useNext) {
$entity | Select-Object `
id,
@{ Name = 'RatedOI'; Expression = {
$_.characteristics.RatedOI.boolValue }
}
#break
$useNext = $false
}
}
}
If I understand your rules correctly, the following should work; it does yield the desired output with your sample JSON:
(Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
foreach ($entity in $_.EntitiesData.Entities) {
if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -isnot [array]) {
$entity | Select-Object `
id,
@{ Name = 'RatedOI'; Expression = {
$_.characteristics.RatedOI.boolValue }
}
}
}
}
Note that IHUR
is not checked for above, because I understood your rules to mean that that is no longer necessary.