首页 > 解决方案 > Powershell - 从 JSON 数组返回特定​​行的值

问题描述

很抱歉不得不在这里问这样的问题,自学的 powershell 用户。有一个问题,我似乎无法从 GET Invoke-RestMethod 的 JSON 返回中获得我想要的值。

我正在尝试提取 JIRA 票证数据,以便检查自定义字段是否已使用值更新。我尝试直接在响应中调用自定义字段,但是由于响应的格式设置它只会返回数据列,因此我无法在我的代码中使用此响应。

我的代码如下:

$response = Invoke-RestMethod "https://jira-ceg.atlassian.net/rest/servicedeskapi/request/$JIRATICKET" -Method 'GET' -Headers $headers 

$tester = $response.requestFieldValues


$tester

我从 JIRA 得到的回复如下:

    fieldId           label              value                                                                                                                                                   
-------           -----              -----                                                                                                                                                   
summary           Summary            STAFF-NEW - XXXX- NAME 
customfield_10108 iTrent Information Employee ID: XXXX...                                                                                                                              
customfield_10086 Checklist Text     --- ERROR - No Role Entitlement Found - Contact Service Admin                                                                                           
description       Description        A new employee has joined. Please process with the data above.                                                                                          
customfield_10191 New AD Address     XXXX@XXXXX.com                                                                                                                
                                                                                                                                                

我想捕获字段 customfield_10191 的值,该字段有时也可能是空白的,稍后我将在脚本中对其进行测试。知道我该怎么做吗?

提前致谢!

标签: powershellrestjira-rest-apiinvoke-restmethod

解决方案


如果$tester是具有属性fieldIdlabel和的对象value,则可以使用成员访问(语法object.property)和Where-Object的组合:

($tester | Where fieldId -eq 'customfield_10191').value

选择对象也适用:

$tester | Where fieldId -eq 'customfield_10191' | Select-Object -Expand value

要返回包含特定客户的对象:

$tester | Where fieldId -eq 'customfield_10191'

推荐阅读