mesarth / JSONPath-Notebook

Execute JSONPath queries inside VS Code Notebooks
https://marketplace.visualstudio.com/items?itemName=tschranz.jsonpath-notebook
MIT License
2 stars 0 forks source link

Implement *~ operation #14

Closed drodiger closed 4 months ago

drodiger commented 5 months ago

$. usually returns all objects $.~ should return only first children key names (not values). $.data.~ would return all children key names under data. $..*~ would return all second level key names.

jsonpath.com has this implemented, you can test.

With this operation it is easier to analyse data in json.

mesarth commented 5 months ago

This syntax is not part of the JSONPath standard. I don't think it's a good idea to add additional operations on top of the standard.

Maybe we can add a toggle button to the notebook cell toolbar to switch between values and key names.

drodiger commented 5 months ago

That would also help. Or switch between standard JSON and non standard ;-)

jg-rp commented 5 months ago

Version 1.2.0 of JSON P3 includes a non-standard keys selector that might be of interest to you (docs).

It is disabled by default. Set the strict option to false when constructing a JSONPathEnvironment to enable all non-standard features.

When enabled, the keys selector defaults to ~. You can change it to *~ by setting the keysPattern option to /\*~/y.

import { JSONPathEnvironment } from "json-p3";

const standardEnv = new JSONPathEnvironment();
const nonStandardEnv = new JSONPathEnvironment({ strict: false, keysPattern: /\*~/y });
const data = {data: {hello: "foo", world: "bar"}};

console.log(nonStandardEnv.query("$.data.*~", data).values());
// [ 'hello', 'world' ]

standardEnv.query("$.data.*~", data);
// JSONPathSyntaxError: expected '.', '..' or a bracketed selection, found '~' ('$.data.*~':8)
mesarth commented 4 months ago

Thank you @jg-rp! That's great!

I've added an icon button in the cell's status bar to switch between standard and non-standard syntax modes.

scrennshot

The default mode can be changed in the settings. image

Does this solve your problem @drodiger?

drodiger commented 4 months ago

Yes, that would be great. Thanks

mesarth commented 4 months ago

@drodiger this is now available with version 2.2.0

drodiger commented 4 months ago

Tested 2.2.0. Works great. You can close the request. Thanks