molsonkiko / JsonToolsNppPlugin

A Notepad++ plugin providing tools for JSON like linting, querying, a tree view, and CSV conversion.
Apache License 2.0
85 stars 9 forks source link

Assistance with RemesPath #19

Closed AkikoOrenji closed 1 year ago

AkikoOrenji commented 1 year ago

Sorry for perhaps a basic question but i have a json document that i can filter down on all the Objects i'm after using @.tracks[:].objects[:] however i wish to only show the objects where a value in one of the keys within those object equals a text string.

I noticed i can do something like this with jmespath using a filter projection e.g. machines[?state=='running'].name (from their examples). I can't see anything in the plugin documentation for this and when i try things like the following in the plugin query box i get unexpected character errors e.g. @.tracks[].objects[][?type=='vehicle'] or @.tracks[*].objects[?type=='vehicle']

Thanks in advance for any advice you can offer

molsonkiko commented 1 year ago

Hi @AkikoOrenji , while RemesPath is JMESPath-inspired, there are a number of substantial departures from JMESPath syntax, as you've experienced.

Try working with this JSON:

{
    "tracks": [
        {"objects": [
                {"type": "vehicle", "name": "car"},
                {"type": "animal", "name": "dog"}
            ]
        },
        {"objects": [
                {"type": "vehicle", "name": "boat"},
                {"type": "vegetable", "name": "spinach"}
            ]
        }
    ]
}

and consider the query @.tracks[:].objects[:][@.type == vehicle]

This query will produce the JSON

[
    [
        {"type": "vehicle", "name": "car"}
    ],
    [
        {"type": "vehicle", "name": "boat"}
    ]
]

because the query's equivalent pseudocode is

let output = list()
let this = this['tracks'] # @.tracks
for each thing in this: # [:]
    let suboutput = list()
    let this = this['objects'] # .objects
    for each thing in this: # [:]
        if this['type'] == 'vehicle': # [@.type == vehicle]
            suboutput.append(this)
    if suboutput is not empty:
        output.append(suboutput)
return output

Note that while in this example, I used unquoted strings, in general RemesPath uses backticks to enquote strings, like so: @.`this is a quoted string`.

AkikoOrenji commented 1 year ago

This is perfect. Thanks