cburgmer / json-path-comparison

Comparison of the different implementations of JSONPath and language agnostic test suite.
https://cburgmer.github.io/json-path-comparison/
GNU General Public License v3.0
86 stars 26 forks source link

Determine support for "terminal tilde" for extracting keys #151

Open hattesen opened 2 weeks ago

hattesen commented 2 weeks ago

Quite a few JSONPath implementations support "terminal tilde" notation to extract object keys (rather than values), but I have come across issues raised on a few, that do NOT support the "terminal tilde" notation, while searching for any formal description of the notation.

It would be nice to have the "terminal tilde" support included in the json-path-comparison overview.

Examples evaluated using JSONPath online evaluator:

{
    "firstName": "John",
    "lastName": "doe",
    "age": 26,
    "address": {
        "streetAddress": "naist street",
        "city": "Nara",
        "postalCode": "630-0192"
    },
    "phoneNumbers": {
        "mobile": {
            "type": "iPhone",
            "number": "0123-4567-8888"
        },
        "work": {
            "type": "land line",
            "number": "0123-4567-8910"
        }
    }
}

To extract the keys of the address object, you would evaluate the JSONPath expression $.address.*~ or $.address[*~] resulting in:

[
  "streetAddress",
  "city",
  "postalCode"
]

To extract the keys of the phoneNumbers object, you would use the JSONPath expression $.phoneNumbers.*~ or $.phoneNumbers[*~], resulting in:

[
  "mobile",
  "work"
]

The only specification documentation of the "terminal tilde" JSONPath notation that I have been able to locate is https://github.com/json-path/JsonPath#functions

Function: keys() Description: Provides the property keys (An alternative for terminal tilde ~) Output type: Set<E>

hattesen commented 2 weeks ago

Here are a couple of links to issues regarding keys() and "terminal tilde" support in the JsonPath GitHub repository:

danielaparker commented 2 weeks ago

@hattesen, I agree that it should be included in the comparisons, it's supported in the most widely used javascript implementation, I'd suggest submitting a pull request that covers this feature for @cburgmer's consideration.

Fork the repository and in the queries directory, create a new subdirectory with a name that follows the conventions, something like "terminal_tilde_for_extracting_keys". Include two files, document.json and selector, that contain the JSON document and JSONPath selector respectively.