1

I have similar JSONs like this and I want parse these JSONs with Powershell.

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }....

I can do it with command $angularJsonPSO.projects."my-app".projectType for every single JSON but I want some generic command for all these JSONs. Something like "...projects.*.projectType...". How is possible do it this way ?

$angularJson = Get-Content -Raw -Path "./angular.json"
$angularJsonPSO = $angularJson | ConvertFrom-Json -Depth 10
$projectName = $angularJsonPSO.projects."my-app".projectType

Thank you very much for any answer.

LDonSOvrfw
  • 21
  • 2
  • `$projectName = $angularJsonPSO.projects | ForEach-Object { $_.PsObject.Properties.Value.projectType }` – Theo Oct 21 '22 at 11:15
  • As an aside: There is usually no need for `-Depth` when _reading_ JSON (`ConvertFrom-Json` - except if your JSON has more than 1024(!) nesting levels). `-Depth` is only important when _writing_ JSON, however (`ConvertTo-Json`), because the default depth is then, unfortunately, 2. See [this answer](https://stackoverflow.com/a/72660856/45375) for more information. – mklement0 Oct 21 '22 at 11:32

1 Answers1

2

Use the hidden psobject memberset to access each property of the projects object:

$angularJson = Get-Content -Raw -Path "./angular.json"
$angularJsonPSO = $angularJson | ConvertFrom-Json -Depth 10

foreach($projectProperty in $angularJsonPSO.projects.psobject.Properties) {
  $projectName = $projectProperty.Name
  $project = $projectProperty.Value

  # now you can access projectType like this
  $project.projectType
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206