json 在Powershell中访问复杂嵌套对象键(键名称)

alen0pnh  于 2023-05-19  发布在  Shell
关注(0)|答案(1)|浏览(213)

我尝试将我的JSON数据转换为特定的ArrayList

我的示例JSON文件

$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" ]
          }
     }]

  }]
}
'@

我的函数捕捉第一个入口

function Get-FirstPropertyValue($obj, $propName) {
  $propNames = $obj.psobject.properties.Name
  if ($propName -in $propNames) {
    $obj.$propName
  } 
  else {
    foreach ($iterPropName in $propNames) {
        if ($null -ne ($val = Get-FirstPropertyValue $obj.$iterPropName $propName) ) {
          return $val
        }
      }
  }
}
$data = $personData | ConvertFrom-Json
$person = Get-FirstPropertyValue $data 'persons'
$propName = @($person.psobject.properties.Name)[0]

write-Host $propName
@($person.psobject.properties.Name)[0].Keys       # Nothing

Q:如何只访问嵌套的对象名?

My Expected Output:
------------------
family_adress
childiren
years_activity

我想访问,person > johndoe > subkeys ->(如果subkeys没有多个属性,则捕获其唯一的名称,因为我想将它们添加到数组列表中)

b4lqfgs4

b4lqfgs41#

JSON看起来格式不正确,persons不是一个数组,而是一个结构体,所以用[0]索引不会选择johndoe。这使它成为一个结构体数组:

$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" ]
      }
   }}]
}
'@

这将选择第一个用户(johndoe)及其属性名称:

$persons = Get-FirstPropertyValue $data 'persons'
$person = $persons[0]
write-host "person: " ($person | out-string)
$value = $person.psobject.properties.Value
write-host "props: " $value.psobject.properties.name # props:  id family_adress childiren years_activity

相关问题