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.

2
  • $projectName = $angularJsonPSO.projects | ForEach-Object { $_.PsObject.Properties.Value.projectType } Commented Oct 21, 2022 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 for more information. Commented Oct 21, 2022 at 11:32

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.