0

I have a Json-response stored as a powershell object if I convert it back to JSON it looks like this:

    [
  {
    "Guid": "69613454-c846-41c8-b4f2-e29a086c93c0",
    "object": [
      {
        "id": "1",
        "value": "Example 1"
      },
      {
        "id": "2",
        "value": "Example 2"
      },
      {
        "id": "3",
        "value": "Example 3"
      }
    ]
  },
  {
    "Guid": "507610ac-fe09-46bb-ad3d-7948008e5687",
    "object": [
      {
        "id": "1",
        "value": "Example 4"
      },
      {
        "id": "2",
        "value": "Example 5"
      },
      {
        "id": "3",
        "value": "Example 6"
      }
    ]
  }
]

And if I list the powershell object ($jsonresponse)

Guid                                 object
----                                 ------
69613454-c846-41c8-b4f2-e29a086c93c0 {@{id=1; value=Example 1}, @{id=2; value=Example 2}, @{id=3; value=Example 3}}
507610ac-fe09-46bb-ad3d-7948008e5687 {@{id=1; value=Example 4}, @{id=2; value=Example 5}, @{id=3; value=Example 6}}

So I'd like to list only two columns, one containing the Guid and the second only the value that belongs to id 1.

Guid                                 object
----                                 ------
69613454-c846-41c8-b4f2-e29a086c93c0 Example 1
507610ac-fe09-46bb-ad3d-7948008e5687 Example 4

Tried $jsonresponse | Select-Object -Property Guid -ExpandProperty Object

but get Select-Object: The property cannot be processed because the property "guid" already exists.

SuperDOS
  • 301
  • 1
  • 3
  • 16
  • 1
    How about `| ConvertFrom-Json | Select-Object -Property Guid -ExpandProperty Object` ? – Olaf Sep 27 '22 at 07:09
  • Yes that almost works, I guess I can then do a for loop to only select id 1 – SuperDOS Sep 27 '22 at 07:13
  • How about a `Where-Object`? ;-) – Olaf Sep 27 '22 at 07:14
  • Hmm I get Select-Object: The property cannot be processed because the property "guid" already exists. – SuperDOS Sep 27 '22 at 07:18
  • Please update your question and show the code you use. – Olaf Sep 27 '22 at 07:29
  • 1
    If I do this `| ConvertFrom-Json | Select-Object -Property Guid -ExpandProperty Object | Where-Object -Property id -eq -Value 1` on the here string I created from your sample JSON data the output looks almost exactly like you want it. – Olaf Sep 27 '22 at 07:31
  • Does this answer your question: [Convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/a/46081131/1701026)? `$Json |ConvertFrom-Json |Flatten-Object |Format-Table` – iRon Sep 27 '22 at 07:46
  • @Olaf it works first time but if I run it again I get Select-Object: The property cannot be processed because the property "guid" already exists. – SuperDOS Sep 27 '22 at 08:13
  • Did you follow the link iRon shared? And please update your question with the COMPLETE code you're using. I cannot reproduce your issue. – Olaf Sep 27 '22 at 08:45
  • @olaf you solution works, it was the powershell extension in vscode that started to act up :) – SuperDOS Sep 27 '22 at 08:54

0 Answers0