Search code examples
javajsonpath

List all the paths which contains an attribute


I have following JSON, in which I want to list all parent node paths where path attribute is present.

{
        "ServiceId": {
            "type": "string",
            "admin": "false"
        },
        "NormalizedEvents": {
            "type": "list",
            "path": "/data/../../nEvents"
        },
        "Events": {
            "type" : "list",
            "path": "/data/../../Events"
        }
    }

In this case, I need output like, $.NormalizedEvents and $.Events.


Solution

  • use JSONPath options to output path instead of value.

    JSONPath

    $.[*][?(@.path)]
    

    Output

    [
       "$['NormalizedEvents']",
       "$['Events']"
    ]
    

    JsonPath expressions can use the dot–notation $.NormalizedEvents

    or the bracket–notation $['NormalizedEvents']

    Online Tool : Jayway JsonPath Evaluator enter image description here