给定以下JSON:

{
  "Team1": {
    "John Smith": {
      "position": "IT Manager",
      "employees": [
        {
          "name": "John Doe",
          "position": "Programmer"
        },
        {
          "name": "Jane Vincent",
          "position": "Developer"
        }
      ]
    },
    "Jane Smith": {
      "position": "Payroll Manager",
      "employees": [
        {
          "name": "John Bylaw",
          "position": "Clerk"
        },
        {
          "name": "Jane Tormel",
          "position": "accountant"
        }
      ]
    }
  },
  "Team2": {
    "Bob Smith": {
      "position": "IT Manager",
      "employees": [
        {
          "name": "Bob Doe",
          "position": "Programmer"
        },
        {
          "name": "Margareth Smith",
          "position": "Developer"
        }
      ]
    },
    "Mary Smith": {
      "position": "Payroll Manager",
      "employees": [
        {
          "name": "Henry Bylaw",
          "position": "Clerk"
        },
        {
          "name": "Eric Tormel",
          "position": "accountant"
        }
      ]
    }
  }
}

我想搜索一个员工的名字,然后返回他/她所在的团队及其经理.比如Henry Bylaw,我想说他的经理是Mary Smith,他们在2队.显然,比这个样本更多的是2个队和更多的经理.

我将JSON放在一个变量中:

$jsonData = '{
    "Team1": {
        "John Smith" : {
.... Too long to post here but see above.
        }
    }   
}

这样做:

# Convert JSON string to PowerShell object
$data = $jsonData | ConvertFrom-Json

# Function to find What team is on
function FindTeam($employeeName) {
    $team1 = $data.Team1 | Where-Object { $_.employees | Where-Object { $_.name -eq $employeeName } }
    $team2 = $data.Team2 | Where-Object { $_.employees | Where-Object { $_.name -eq $employeeName } }

    if ($team1) {
        return "Team #1"
    } elseif ($team2) {
        return "Team #2"
    } else {
        return "Not found on any teams"
    }
}

# Call the function
$employeeName = "Jane Vincent"
$result = FindTeam $employeeName
Write-Output "$employeeName is on $result"

这将导致:

Jane Vincent is on Not found on any teams

正如您所看到的,这不起作用,也不能扩展到数百个团队. 不,我不能更改JSON的 struct . 我怎么能这样做呢?

推荐答案

这是一个非常难处理的Json,除了指出.PSObject.Properties可以用来反射对象以获取其属性(名称和值)之外,我不确定如何解释其逻辑.

$json = Get-Content .\test.json -Raw | ConvertFrom-Json
$target = 'Henry Bylaw'

foreach ($team in $json.PSObject.Properties) {
    foreach ($manager in $team.Value.PSObject.Properties) {
        foreach ($employee in $manager.Value.employees) {
            if ($employee.name -eq $target) {
                # `return` is used here to exit the loop,
                # if we found `$target` there is no need to keep searching
                return [pscustomobject]@{
                    Emplyee  = $employee.name
                    Position = $employee.position
                    Team     = $team.name
                    Manager  = $manager.name
                }
            }
        }
    }
}

你可以从上面的代码中得到一个对象,它包含该员工的所有详细信息($target):

Emplyee     Position Team  Manager
-------     -------- ----  -------
Henry Bylaw Clerk    Team2 Mary Smith

Json相关问答推荐

基于两个条件替换扁平化的SON中的值

JSON字符串处理注入引号

我可以使用JQ来缩小数组中的json对象的范围吗?

使用快速json库编写json可以消除所有缩进

在深度嵌套数组中使用布尔属性的jq-select

基于 JSON 字段的 Jolt 条件标志

使用 JOLT 将日期格式转换为 JSON

Android 如何判断小时时间是否在两个时间之间?

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

如何使用 jq 将字符串数组转换为对象?

ETCD 导出为 json 并从 base64 解码所有键/值到人类可读

无法在Kotlin 中解析JSONObject

如何在循环中传递列表(会话(字符串属性))以在 Gatling Scala 中创建批量 Json 请求

TSQL FOR JSON 嵌套值

Microsoft GRAPH 查询使用端点 /deviceManagement/deviceHealthScripts 列出了一种不熟悉的检测脚本内容格式

如何判断字符串是否为json格式

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

与classic 规范化表相比,postgres JSON 索引是否足够高效?

如何从 JSON 响应中提取单个值?

JSON 格式的 Amazon S3 响应?