Search code examples
javajsonjayway

How to get json path in java when the json value has hyphen


How to get json path in java when the json value has hyphen?

I have com.jayway.jsonpath.InvalidPathException.

Error message:

com.jayway.jsonpath.InvalidPathException: Expected character: )

    at com.jayway.jsonpath.internal.CharacterIndex.readSignificantChar(CharacterIndex.java:218)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalANDOperand(FilterCompiler.java:185)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalAND(FilterCompiler.java:151)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.readLogicalOR(FilterCompiler.java:131)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:77)
    at com.jayway.jsonpath.internal.filter.FilterCompiler.compile(FilterCompiler.java:53)
    at com.jayway.jsonpath.internal.path.PathCompiler.readFilterToken(PathCompiler.java:461)
    at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:141)
    at com.jayway.jsonpath.internal.path.PathCompiler.readPropertyOrFunctionToken(PathCompiler.java:237)

JSON value:

    {
  "data": {
    "storeGtin": {
      "gtin": "64491100727",
      "countryCode": "IN",
      "storeId": "100",
      "assignmentInfo": {
        "capacity": 118,
        "featureFlag": false,
        "assignedLocations": [
          {
            "capacity": 52,
            "featureFlag": false,
            "storeLocation": {
              "sgln": "11",
              "sgln195": "11.F.E.25.1",
              "legacyId": "101035402"
            },
            "assignments": [
              {
                "capacity": 66,
                "featureFlag": false,
                "storeLocation": {
                  "sgln": "5577557755775100001022310",
                  "sgln195": "5577557755775W.SF.D.26.1",
                  "legacyId": "101246052"
                },
                "assignments": [
                  {
                    "gtin": "64491100727",
                    "type": "AASASA",
                    "assigner": "SASAQW",
                    "assignTs": "2023-04-12T17:14:42.880Z",
                    "eslTag": {
                      "barcode": "w422-5r72",
                      "vendor": "CMK"
                    },
                    "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"
                        }
                      }
                    }
                  }
                ]
              }
            ]
          }
        }
      }
    }

JSON Path:

$.data.storeGtin.assignmentInfo.assignedLocations[*].assignments[?(@.barcode='w422-5r72' && @.eslTag!='null')].modular.category.department.departmentNumber

Solution

  • The following path expression finds the value 17 for departmentNumber, in the corrected (valid) version of your JSON. The result is actually a JSONArray - but it only contains 1 value - the 17.

    You can compare it with your version to see the differences - and decide for yourself if this is what you do actually need.

    $.data.storeGtin.assignmentInfo.assignedLocations[*].assignments[*].assignments[?(@.eslTag != null && @.eslTag.barcode == 'w422-5r72')].modular.category.department.departmentNumber
    

    Basically:

    • the original expression was missing a level for the 2nd assignments array.
    • eslTag is an object in your sample, so I assume the comparison should be against null not 'null'.
    • the barcode comparison should refer to eslTag.barcode, and not simply barcode.
    • the "equals to" operator is ==, not =.

    Reference.


    The point in the question about "when the value has a hyphen" was not actually relevant to the problem.