【问题标题】:Powershell - Getting values from JSON array return for specific rowPowershell - 从 JSON 数组返回特定​​行的值
【发布时间】:2020-08-13 15:46:41
【问题描述】:

很抱歉不得不在这里提出这样的问题,自学的 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 的值,该字段有时也可能为空,稍后我将在脚本中进行测试。知道我该怎么做吗?

提前致谢!

【问题讨论】:

    标签: powershell rest jira-rest-api invoke-restmethod


    【解决方案1】:

    如果$tester 是具有fieldIdlabelvalue 属性的对象,则可以使用member access(语法object.property)和Where-Object 的组合:

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

    Select-Object 也可以:

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

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

    $tester | Where fieldId -eq 'customfield_10191'
    

    【讨论】:

    • 谢谢!我现在使用 select-object 方法设法让它工作。我的标题仍然进入变量时遇到了问题,但在这里找到了另一篇提到 -ft removeheader 的文章,它完全解决了我的问题。谢谢你,脚本现在运行良好!
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多