Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering recursive by key? #34

Open
kdezz opened this issue Aug 16, 2019 · 4 comments
Open

Filtering recursive by key? #34

kdezz opened this issue Aug 16, 2019 · 4 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@kdezz
Copy link

kdezz commented Aug 16, 2019

I've a situation in which I don't know the keys, for example I have a structure that might look something like this:

{
	"robots": {
		"random_robot_id_39422": {
			"robot_information": 8338
		},
		"random_robot_id_39434": {
			"robot_information": 8113
		},
		"random_robot_id_23823": {
			"robot_information": 8058
		},
		"random_robot_id_97434": {
			"robot_information": 3328
		},
		"random_robot_id_17343": {
			"robot_information": 1001
		}
	}
}

What would be the best way to filter by the ID of the key? For example, if I wanted to retrieve the random_robot_id_17343 node.

@dmlb2000 dmlb2000 added good first issue Good for newcomers question Further information is requested labels Aug 16, 2019
@markborkum
Copy link
Contributor

@kdezz, we are actively developing the "callables" capability for exactly this use case (see #14 for details).

For example, using the entries() and match(regexp) functions:

$["robots"][entries()][?(@[0][match("_17343$")])][1]

The above JSONPath tests the key-value pairs of the "robots" JSON object for keys that end with "_17343" and then extracts the corresponding value.

Please note that the match(regexp) function is not yet implemented.

@grandprixgp
Copy link

Am I right in thinking that match() should return True/False to satisfy the ? operator?

@markborkum
Copy link
Contributor

markborkum commented Sep 7, 2019

@grandprixgp The ? operator is satisfied if a subscript yields a JSON value (i.e., logical disjunction). The match callable yields a JSON array representing the match data (with the same semantics as the MatchData class in the Ruby standard library). For example:

$["string"][?(@[match("pattern")])]

yields all "string"s that match the given "pattern", and

$["string"][match("pattern")][?(@[1][parseInt()] > 42)][2]

yields the second capture group of "pattern" applied to "string" if the first capture group, cast to an integer, satisfies a predicate.

Please note that the parseInt function is not yet implemented.

(Edit: Add closing square bracket to second example.)

@grandprixgp
Copy link

Thanks for clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants