jsonpowershellobject-graph

PowerShell code returning results when 1 of the 2 variables are not found in JSON . Looking to enhance program


Good morning. Stack Overflow, Powershell, Object Graph novice here.

I am interested in making an enhancement to the following program in Powershell. As of now, the program searches for "RatedOI" and entityType "Operator" inside of a ForEach. Once "Operator" is found, the program yields two objects of interest: id and RatedOI.

I have a complex JSON file where id and RatedOI appear in the JSON file. If RatedOI is not found in JSON file, the program still returns the id string. Is it possible to modify the code where no results yield for id and RatedOI when RatedOI is not found?



expected output
 id                                    RatedIO 
----                                  ------
6fe06340-072f-49b2-8d5a-449e3db80002   False

Actual Output
  Id                                     RatedOI
  ----                                  ------
  7af50dbb-7ea3-4458-adfe-a82198a80001
  7af50dbb-7ea3-4458-adfe-a82198a80002  False  

Can someone please review this sample JSON file and PowerShell code and provide feedback?
JSON code

 {
"id": "e0b8a6aa-2675-4d93-86ff-6a51a0daea85",
"_etag": "\"ba00b86de-0000-0100-00dd00-671029b20000",
"LOB": "A",
  "RD": {
  "PSM": {
  },
"entities": [
  {
  }
],
"qid": "638ee347-42ed-441c-a6ff-d6c99cf62df55",
"peakData": {
},
"PDR": []
},
"ASC": "Committed",
"entitiesData": {
"entities": [
  {
  },
  {
  "id": "b0000000-0000-0000-0000-00000000000b",
  "entityType": "InsuranceHistoryUserResponse",
  "ordinal": 0,
  "characteristics": {
  "PBILR": {
  "stringValue": "XYZ"
  },
  "PBILRC": {
    "stringValue": "XYZ"
  },
  "PISR": {
  "stringValue": "XeYZ"
  }
                    }     
  },
  {
    "id": "7af50dbb-7ea3-4458-adfe-a82198a80001",
    "entityType": "Operator",
    "ordinal": 0,
    "characteristics": {
      "FirstName": {
        "stringValue": "Joe"
      },
      "ODLNUR": {
        "stringValue": "A82539878"
      },
      "RatedOI2": {
        "boolValue": true
      }
    }
   },
   {
    "id": "b0000000-0000-0000-0000-00000000000",
    "entityType": "IHUR",
    "ordinal": 0,
    "characteristics": {
    "PBILR": {
    "stringValue": "XYZ"
    },
    "PBILRC": {
      "stringValue": "XYZ"
    },
    "PISR": {
    "stringValue": "XYZ"
    }
                      }     
    },
    {
      "id": "7af50dbb-7ea3-4458-adfe-a82198a80002",
      "entityType": "Operator",
      "ordinal": 0,
      "characteristics": {
        "FirstName": {
          "stringValue": "Joe"
        },
        "ODLNUR": {
          "stringValue": "3333"
        },
        "RatedOI": {
          "boolValue": false
        }
      }
     }

     ]
   }
   }

PowerShell code here, 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 {
  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 } 
        }
    }
  }
}

Solution

  • Add -and $entity.Characteristics.RatedOI to your if-statement conditional:

    # ...
    (Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
      foreach ($entity in $_.EntitiesData.Entities) {
        # Note the addition of `-and $entity.Characteristics.RatedOI`
        if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -isnot [array] -and $entity.Characteristics.RatedOI) {
          $entity | Select-Object `
            id, 
            @{ Name = 'RatedOI'; Expression = { 
                $_.Characteristics.RatedOI.boolValue } 
            }      
        }
      }
    }
    

    The above takes advantage of PowerShell's implicit to-Boolean conversion: