1
$personData = @'
{
  "persons": [{
      "johndoe": [{
          "id": "4334234 <>",
         
          "family_adress": {
            "mother": "address of family",
            "father": "",
            "brother": "address of family"
          },
          
          "childiren": {
            "first": 1024,
            "second":128
          },

          "years_activity": {
              "2021":  [ "sample entry-1", "sample entry-2" ],
              "2022":  [ "sample entry-1", "sample entry-2" ]
          }
     }],

      "johndoe2": [{
          "id": "4334234 <>",
         
          "family_adress": {
            "mother": "address of family...",
            "father": "",
            "brother": "...address of family"
          },
          
          "childiren": {
            "first": 25,
            "second": 12
          },

          "years_activity": {
              "2021":  [ "sample entry-4", "sample entry-r" ],
              "2022":  [ "sample entry-5", "sample entry-s" ]
          }
     }]

  }]
}
'@ | ConvertFrom-Json
  • How can I access: persons.JohnDoe.years_activity values and add them to seperate array? if last property object is "System.Object[]"

For example:

johnDoeYears_Activity = @{}
johnDoeYears_Activity2021 [
   "2021", [ "sample entry-1", "sample entry-2" ],
   "2022", [ "sample entry-1", "sample entry-2" ],
]
  • OR can I make it with any seperator with combine them in a single value for future usage?

For Example:

johnDoeYears_Activity = @{}
johnDoeYears_Activity2021 [
   "2021", "sample entry-1, sample entry-2",
   "2022", "sample entry-1, "sample entry-2"
] 

Desired Output:

Name: 2021
Value: sample entry-1, sample entry-2    
(Normally it prints without any delimiter, value will come from for loop and combined with ",")
Ichigo Kurosaki
  • 135
  • 1
  • 6
  • Can you clarify what the desired output is? – Paolo May 20 '23 at 12:50
  • So you want `johnDoeYears_Activity` to be an array of objects? – Paolo May 20 '23 at 12:57
  • Yes, but I want to achieve this purpose iterate all objects – Ichigo Kurosaki May 20 '23 at 12:58
  • PowerShell objects work different than Python ones. Can you have a look at `$personData.persons[0].johndoe[0].years_activity[0]` snippet and check the values? You can access them directly now too. But instead of using a dictionary as a backend, you can see arrays are used for nested objects. – Zafer Balkan May 21 '23 at 14:05

1 Answers1

0

The code below:

$years_activity = $personData.persons[0].johndoe[0].years_activity
$years = $years_activity  | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name

foreach ($year in $years) {
    Write-Host "Name: $year"
    $value = $years_activity.$year
    $valueStr = [string]::Join(", ", $value)
    Write-Host "Value: $valueStr"
    Write-Host "-----------------------------"
}

produces the following output:

Name: 2021
Value: sample entry-1, sample entry-2
-----------------------------
Name: 2022
Value: sample entry-1, sample entry-2
-----------------------------

Does this answer your question?