如何使用JSONPath获取JSON数组的parent属性

v7pvogib  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(294)

eslTag = "null"时需要取sgln195的值。如何根据json数组上的值获取以前的节点。
我在代码中使用Jayway JSONPath库

{
  "data": {
    "storeGtin": {
      "gtin": "64491100727",
      "countryCode": "US",
      "storeId": "5577",
      "assignmentInfo": {
        "capacity": 118,
        "featureFlag": false,
        "assignedLocations": [
          {
            "capacity": 52,
            "featureFlag": false,
            "storeLocation": {
              "sgln": "5577557755775100001035402",
              "sgln195": "5577557755775W.SF.E.25.1",
              "legacyId": "101035402"
            },
            "assignments": [
              {
                "gtin": "64491100727",
                "type": "sales",
                "assigner": "cmk",
                "assignTs": "2023-03-29T15:33:08.668Z",
                "eslTag": "null",
                "sequenceNumber": 1,
                "modularSectionNumber": 1,
                "capacityCnt": 52,
                "replenishmentGroupNumber": "207186183",
                "vertFacingCnt": 10,
                "horizFacingCnt": 10,
                "merchandiseStyleCode": 0,
                "modular": {
                  "modularName": "020FTX078IN BLINDS AND SHADES",
                  "planId": 12912966,
                  "effectiveDate": "2023-01-02",
                  "relayDate": "2023-01-02",
                  "discontinueDate": "null",
                  "status": "Y",
                  "category": {
                    "categoryName": "BLINDS AND SHADES",
                    "categoryNumber": 305,
                    "department": {
                      "departmentNumber": 17,
                      "departmentName": "HOME DECOR"
                    }
                  }
                }
              }
            ]
          },
          {
            "capacity": 66,
            "featureFlag": false,
            "storeLocation": {
              "sgln": "5577557755775100001022310",
              "sgln195": "5577557755775W.SF.D.26.1",
              "legacyId": "101246052"
            },
            "assignments": [
              {
                "gtin": "64491100727",
                "type": "ModItemSalesfloorAssignment",
                "assigner": "esl_test",
                "assignTs": "2023-04-12T17:14:42.880Z",
                "eslTag": {
                  "barcode": "w422-5rhp",
                  "vendor": "vck"
                },
                "sequenceNumber": 2,
                "modularSectionNumber": 2,
                "capacityCnt": 66,
                "replenishmentGroupNumber": "207186183",
                "vertFacingCnt": 10,
                "horizFacingCnt": 10,
                "merchandiseStyleCode": 0,
                "modular": {
                  "modularName": "020FTX078IN BLINDS AND SHADES",
                  "planId": 12912966,
                  "effectiveDate": "2023-01-02",
                  "relayDate": "2023-01-02",
                  "discontinueDate": "null",
                  "status": "Y",
                  "category": {
                    "categoryName": "BLINDS AND SHADES",
                    "categoryNumber": 305,
                    "department": {
                      "departmentNumber": 17,
                      "departmentName": "HOME DECOR"
                    }
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}
s3fp2yjn

s3fp2yjn1#

要按子节点选择父节点,需要在所有级别上使用[?(..)]过滤器表达式。
以下路径在Jayways的JsonPath中有效:

$.data.storeGtin.assignmentInfo.assignedLocations[?(@.assignments[?(@.eslTag == 'null')])].storeLocation.sgln195

代码示例:

String json =  "{...}"; //json-string needs to be escaped
JSONArray jsonArray = JsonPath.read(json,"$.data.storeGtin.assignmentInfo.assignedLocations[?(@.assignments[?(@.eslTag == 'null')])].storeLocation.sgln195");
System.out.println(jsonArray.size());
for (int i = 0; i < jsonArray.size(); i++) {    
    System.out.println(jsonArray.get(i));
}

PS:如果你用一个(基于JS的)JsonPath tester online来测试,你需要使用不同的语法:$.data.storeGtin.assignmentInfo.assignedLocations[?(@.assignments.eslTag = 'null')].storeLocation.sgln195

相关问题