最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 } 
        }
    }
  }
}

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 } 
        }
    }
  }
}
Share Improve this question edited Feb 15 at 17:46 mklement0 439k68 gold badges702 silver badges912 bronze badges asked Feb 15 at 16:15 BigSmooth82BigSmooth82 1077 bronze badges 1
  • 2 FYI, I build a general ObjectGraphTool module that might help you to discover these type of object-graphs or even select you value in a single command (Get-Content -Raw $file | ConvertFrom-Json | Get-Node ~entityType=Operator..characteristics.RatedOI.boolValue=true....id).value. For details on this example, see: Get-Node and Extended Dot Notation. – iRon Commented Feb 16 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 1

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:

  • In a Boolean context - such as an if conditional - an object of any type can be used, which PowerShell then interprets as a Boolean.

  • Thus, $entity.Characteristics.RatedOI evaluates to $true only if a .RatedOI property with a "truthy" value is present.

    • In your case, if a .RatedOI property is present, it, contains a nested object, which ConvertFrom-Json parses into a [pscustomobject]. Any [pscustomobject] instance is coerced to $true (even one without properties; try [bool] [pscustomobject] @{ }.
  • In the absence of a .RatedOI property, PowerShell by default quietly returns $null when an attempt is made to such a non-existent property, and $null is coerced to $false (try [bool] $null).

    • A small caveat is that the quiet to-$null evaluation can be changed to reporting an error instead, namely if Set-StrictMode -Version 2 or higher is in effect.

    • You can guard against this by either placing Set-StrictMode -Off or Set-StrictMode -Version 1 (if you want errors to be reported for non-existent variables, which by default also evaluate to $null) at the start of your script / function.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论