0

I referenced this answer question,

My Sample Data

$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

My Output:

NamePath                                      Value
--------                                      -----
persons[0].johndoe[0].id                      4334234 <>
persons[0].johndoe[0].family_adress.mother    address of family
persons[0].johndoe[0].family_adress.father
persons[0].johndoe[0].family_adress.brother   address of family
persons[0].johndoe[0].childiren.first         1024
persons[0].johndoe[0].childiren.second        128
persons[0].johndoe[0].years_activity.2021[0]  sample entry-1
persons[0].johndoe[0].years_activity.2021[1]  sample entry-2
persons[0].johndoe[0].years_activity.2022[0]  sample entry-1
persons[0].johndoe[0].years_activity.2022[1]  sample entry-2
persons[0].johndoe2[0].id                     4334234 <>
persons[0].johndoe2[0].family_adress.mother   address of family...
persons[0].johndoe2[0].family_adress.father
persons[0].johndoe2[0].family_adress.brother  ...address of family
persons[0].johndoe2[0].childiren.first        25
persons[0].johndoe2[0].childiren.second       12
persons[0].johndoe2[0].years_activity.2021[0] sample entry-4
persons[0].johndoe2[0].years_activity.2021[1] sample entry-r
persons[0].johndoe2[0].years_activity.2022[0] sample entry-5
persons[0].johndoe2[0].years_activity.2022[1] sample entry-s
  • I want to access this NamePaths with CSV.

Headers: id family_address childrens years_activitys // My Rows: JohnDoe, JohnDoe2

  • After CSV I want to access pritn all propertiesx,
foreach (person in CSV) {
  id: 
  family_address: 
  childrens: 
  years_activitys: (if header equal to SubArray also print them separately)
}
  • Also can I access these values with structed format like

foreach (activities in person.$username.years_activitys ) { key : value }

I'm trying to do exactly something like this, thanks in advance for your help.

Ichigo Kurosaki
  • 135
  • 1
  • 6

1 Answers1

0

Try following. I created a table with you inputs so I could test. I put the activities in separate column by dates instead of new line

$data = @"
persons[0].johndoe[0].id                      4334234 <>
persons[0].johndoe[0].family_adress.mother    address of family
persons[0].johndoe[0].family_adress.father
persons[0].johndoe[0].family_adress.brother   address of family
persons[0].johndoe[0].childiren.first         1024
persons[0].johndoe[0].childiren.second        128
persons[0].johndoe[0].years_activity.2021[0]  sample entry-1
persons[0].johndoe[0].years_activity.2021[1]  sample entry-2
persons[0].johndoe[0].years_activity.2022[0]  sample entry-1
persons[0].johndoe[0].years_activity.2022[1]  sample entry-2
persons[0].johndoe2[0].id                     4334234 <>
persons[0].johndoe2[0].family_adress.mother   address of family...
persons[0].johndoe2[0].family_adress.father
persons[0].johndoe2[0].family_adress.brother  ...address of family
persons[0].johndoe2[0].childiren.first        25
persons[0].johndoe2[0].childiren.second       12
persons[0].johndoe2[0].years_activity.2021[0] sample entry-4
persons[0].johndoe2[0].years_activity.2021[1] sample entry-r
persons[0].johndoe2[0].years_activity.2022[0] sample entry-5
persons[0].johndoe2[0].years_activity.2022[1] sample entry-s
"@

#Create Table for Testing to duplicate OPs input
$reader = [System.IO.StringReader]::new($data)
$table = [System.Collections.ArrayList]::new()
while(($line = $reader.ReadLine()) -ne $null)
{
   $splitArray = $line.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
   $newRow = [PSCustomObject]@{
      NamePath = $splitArray[0] 
      Value = $splitArray[1]
   }
   $table.Add($newRow) | Out-Null
}
$table

#start of real code
$people = [System.Collections.ArrayList]::new()
$name = ""
foreach($row in $table)
{
   $splitArray = $row.NamePath.Split('.')
   $newName = $splitArray[1]
   if($name -ne $newName)
   {
      $newRow = [PSCustomObject]@{
         Name = $newName 
         ID = $row.Value
      }
      $people.Add($newRow) | Out-Null
      $name = $newName
   }
   else
   {
      if($splitArray[2] -ne 'years_activity')
      {
         $pName = $splitArray[2] + '_' + $splitArray[3]
         $newRow | Add-Member -NotePropertyName $pName -NotePropertyValue $row.Value
      }
      else
      { 
         $newRow | Add-Member -NotePropertyName $splitArray[3] -NotePropertyValue $row.Value
      }
   }
}
$people
$people | export-csv 'c:\temp\test.csv'
jdweng
  • 33,250
  • 2
  • 15
  • 20