如何查找与特定过滤器匹配的 JSON 路径元素
How to Find JSON Path element that matches a specific filter
我只需要从下面的数据中获取与特定 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"
}
$.StatementLine[?(@.StatementLineID=='780e0c0f-f62f-42f8-96ad-9a2db62e6271')]
首先:
$.StatementLine
是包含所需元素的数组的路径。
我们可以只说 $.StatementLine[2]
,但如果您事先不知道索引,则需要过滤该数组。
我们用过滤表达式替换索引 2。 ?
是过滤器运算符,因此我们希望用 ?(condition)
替换硬编码的 2,从而得到 $.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
我只需要从下面的数据中获取与特定 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"
}
$.StatementLine[?(@.StatementLineID=='780e0c0f-f62f-42f8-96ad-9a2db62e6271')]
首先:
$.StatementLine
是包含所需元素的数组的路径。
我们可以只说 $.StatementLine[2]
,但如果您事先不知道索引,则需要过滤该数组。
我们用过滤表达式替换索引 2。 ?
是过滤器运算符,因此我们希望用 ?(condition)
替换硬编码的 2,从而得到 $.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