【问题标题】:How to Find JSON Path element that matches a specific filter如何查找与特定过滤器匹配的 JSON 路径元素
【发布时间】:2021-12-16 18:32:42
【问题描述】:

我只需要从下面的数据中获取与特定 ID 匹配的元素(我使用的是 jsonpath.com)。

如果我尝试 $.StatementLine.*[?(StatementLineID='780e0c0f-f62f-42f8-96ad-9a2db62e6271')] 我只会得到所有元素。

{
  "@xmlns:i": "http://www.w3.org/2001/XMLSchema-instance",
  "StatementLine": [
    {
      "Amount": "-1.4000",
      "AnalysisCode": "Fee",
      "BankRuleMatch": {
        "BankRuleID": "08ec6ff4-bb38-45cd-908c-f6c2b3f62bba",
        "BankRuleTemplateInvoiceID": "be462ac0-9be6-45aa-81ee-817248b7023b",
        "ContactFieldToMatchCode": "MATCHFIELD/CONTACT/CONTACTID",
        "LineItemCount": "1",
        "PaidToName": "PayPal",
        "ReferenceFieldToMatchCode": "MATCHFIELD/REFERENCE/REFERENCE",
        "RuleName": "PayPal - Fees",
        "RuleType": "BANKRULETYPE/CASHPAY",
        "StatementLineID": "20ad2616-2d63-4b87-ab4a-49e7c474d40e"
      },
      "ChequeNo": null,
      "Notes": "Fee",
      "Payee": "PayPal (Related to 3BE10345GW6043408)",
      "PostedDate": "01 Nov 2021",
      "StatementID": "93424a06-586e-47c0-92b0-81ea86ff75b9",
      "StatementLineID": "20ad2616-2d63-4b87-ab4a-49e7c474d40e",
      "Type": "DEBIT"
    },
    {
      "Amount": "90.4800",
      "AnalysisCode": "Express Checkout API",
      "ChequeNo": null,
      "MatchedTransactions": {
        "MatchedTransaction": {
          "Amount": "90.4800",
          "IsPayRunTransaction": "false",
          "PaidToName": "Payment: xxxxxxxxxxx",
          "Reconciled": "false",
          "Reference": "xxxxxxxxxxx",
          "StatementLineID": "3346bbcb-3c4a-4985-bf51-a503bf7260a5",
          "SubsidiaryID": "5361e537-39e6-4724-a65c-ef3aed9de1e4",
          "SubsidiaryType": "SUBSTYPE/BANK",
          "TotalCount": "1",
          "TransactionDate": "02 Nov 2021"
        }
      },
      "Payee": "xxxxxxxxxxxxxxxxxxxxx",
      "PostedDate": "01 Nov 2021",
      "Reference": "5XL98374E2648801T",
      "StatementID": "93424a06-586e-47c0-92b0-81ea86ff75b9",
      "StatementLineID": "3346bbcb-3c4a-4985-bf51-a503bf7260a5",
      "Type": "CREDIT"
    },
    {
      "Amount": "-3.6500",
      "AnalysisCode": "Fee",
      "BankRuleMatch": {
        "BankRuleID": "08ec6ff4-bb38-45cd-908c-f6c2b3f62bba",
        "BankRuleTemplateInvoiceID": "be462ac0-9be6-45aa-81ee-817248b7023b",
        "ContactFieldToMatchCode": "MATCHFIELD/CONTACT/CONTACTID",
        "LineItemCount": "1",
        "PaidToName": "PayPal",
        "ReferenceFieldToMatchCode": "MATCHFIELD/REFERENCE/REFERENCE",
        "RuleName": "PayPal - Fees",
        "RuleType": "BANKRULETYPE/CASHPAY",
        "StatementLineID": "780e0c0f-f62f-42f8-96ad-9a2db62e6271"
      },
      "ChequeNo": null,
      "Notes": "Fee",
      "Payee": "PayPal (Related to 5XL98374E2648801T)",
      "PostedDate": "01 Nov 2021",
      "StatementID": "93424a06-586e-47c0-92b0-81ea86ff75b9",
      "StatementLineID": "780e0c0f-f62f-42f8-96ad-9a2db62e6271",
      "Type": "DEBIT"
    }
  ]
}

我需要得到的结果是:

{
  "Amount": "-3.6500",
  "AnalysisCode": "Fee",
  "BankRuleMatch": {
    "BankRuleID": "08ec6ff4-bb38-45cd-908c-f6c2b3f62bba",
    "BankRuleTemplateInvoiceID": "be462ac0-9be6-45aa-81ee-817248b7023b",
    "ContactFieldToMatchCode": "MATCHFIELD/CONTACT/CONTACTID",
    "LineItemCount": "1",
    "PaidToName": "PayPal",
    "ReferenceFieldToMatchCode": "MATCHFIELD/REFERENCE/REFERENCE",
    "RuleName": "PayPal - Fees",
    "RuleType": "BANKRULETYPE/CASHPAY",
    "StatementLineID": "780e0c0f-f62f-42f8-96ad-9a2db62e6271"
  },
  "ChequeNo": null,
  "Notes": "Fee",
  "Payee": "PayPal (Related to 5XL98374E2648801T)",
  "PostedDate": "01 Nov 2021",
  "StatementID": "93424a06-586e-47c0-92b0-81ea86ff75b9",
  "StatementLineID": "780e0c0f-f62f-42f8-96ad-9a2db62e6271",
  "Type": "DEBIT"
}

【问题讨论】:

    标签: jsonpath


    【解决方案1】:
    $.StatementLine[?(@.StatementLineID=='780e0c0f-f62f-42f8-96ad-9a2db62e6271')]
    

    首先:

    $.StatementLine 是包含所需元素的数组的路径。 我们可以直接说$.StatementLine[2],但如果您不提前知道索引,则需要过滤该数组。

    我们将索引 2 替换为过滤器表达式。 ? 是过滤器运算符,因此我们希望将硬编码的 2 替换为 ?(condition),给我们$.StatementLine[?(condition)]

    我们的条件将使用 StatementLineID 和我们要查找的 id,但就像主表达式使用 $. 指示顶级容器一样,过滤器使用 @. 指示当前正在检查的元素。在这种情况下,您可以将 @. 视为数组的每个元素,我们将向其传递 StatementLineID。

    唯一的另一件事是将我们正在寻找的值传递给它。这里有两件事。平等的检验是==,而不是=。此外,字符串值需要用单引号或双引号括起来,因此您的条件是@.StatementLineID=='780e0c0f-f62f-42f8-96ad-9a2db62e6271'。只需将其放入过滤器中,您就会得到整个表达式。

    参考:https://docs.oracle.com/cd/E60058_01/PDF/8.0.8.x/8.0.8.0.0/PMF_HTML/JsonPath_Expressions.htm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-09
      • 2016-03-31
      • 2021-11-13
      • 1970-01-01
      • 2021-01-11
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多