0

Is there a JSONPath expression to filter on keys that match a given pattern ?
I would like to get all the values that contain name in the keys without explicitly listing those keys.

Input

{"foo":{"bar":{"name1":"john","name2":"jane"}}}

Output (expected)

["john","jane"]

Thanks !

py-r
  • 419
  • 5
  • 15
  • @TobiasThieron : You point out to a ticket that is indeed covering the need. Make an answer out of it (saying current library not covering it but proposed one is) and you get the point. Thank – py-r Sep 28 '21 at 15:05
  • @TobiasThieron: I gave you the point for your answer, but it was apparently deleted. Will delete the entire question then. – py-r Oct 06 '21 at 07:22
  • @TobiasThieron: Can't delete the question, which I think makes sense as well. So can you please try to give your answer again and I'll give you the point. – py-r Oct 28 '21 at 20:20
  • @Moderators: pls keep Tobias' answer visible. My question is different as the one asked in the referred ticket. – py-r Oct 28 '21 at 20:22

2 Answers2

1

The vanilla approach to get the value of two keys is to use a $['x', 'y'] union.

In your case this could look like this:

$.foo.bar['name1', 'name2']

Unfortunately, not every JSON path engine will return the result in this way (and there are some minor syntax differences as well, i.e some prefer double quotes or no quotes, etc). E.g. when you test your input with the path above online here: https://jsonpath.herokuapp.com/ Using the Jayway tab yields:

{
   "name1" : "john",
   "name2" : "jane"
}

while the Gatling tab gives the expected result:

[
   "john",
   "jane"
]

So, give it a try with your JSON path engine at hand. If the result is not as expected post-processing the full or intermediate JSON result in a host programing language might be easier.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thanks, but is there a way do proceed without listing all the keys. I've updated the question to make this clear `without explicitly listing those keys`. – py-r Sep 28 '21 at 05:05
  • @py-r This comes down to the JSONpath library or tool you are using and the way it is implemented. As shown in the example most return the key-value pairs when you use a union, others only the values. – wp78de Sep 28 '21 at 13:40
0

If you're having trouble with JsonPath because of the lack of standard syntax and specification, you can use JMESPath instead. It has implementations that pass the TCK in many languages.

foo.bar.[name1, name2]
Stéphane LANDELLE
  • 6,076
  • 2
  • 10
  • 12
  • Thanks, but is there a way do proceed without listing all the keys. I've updated the question to make this clear `without explicitly listing those keys`. – py-r Sep 28 '21 at 05:05
  • I don’t think so, neither with standard JsonPath nor JMESPath. I would argue that your JSON is not designed for this usage or it should have a « names » array instead of « name1 », « name2 », etc attributes. – Stéphane LANDELLE Sep 28 '21 at 05:42
  • Thanks. It was just an example to ask for the feature ;) – py-r Sep 28 '21 at 06:51
  • I think those path based query language are meant for structured data. IMHO, you want to query your data in a non structured way as you want to deal with JSON attribute names as text and not keys. You'll probably have to resort to regex. – Stéphane LANDELLE Sep 28 '21 at 07:02