Closed drodiger closed 4 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.
That would also help. Or switch between standard JSON and non standard ;-)
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)
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.
The default mode can be changed in the settings.
Does this solve your problem @drodiger?
Yes, that would be great. Thanks
@drodiger this is now available with version 2.2.0
Tested 2.2.0. Works great. You can close the request. Thanks
$. 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.