Wild Wild Path – access object property paths using wildcards and regexps ↦
For all your object wrangling needs:
- ⛏️ Dot-delimited paths:
foo.bar.0.baz
- ⭐ Wildcards:
foo.*
,**.bar
- 🗺️ Regexps:
foo./ba?/
- 🏜️ Slices:
foo.0:2
- 🚂 Unions:
foo bar baz
For all your object wrangling needs:
foo.bar.0.baz
foo.*
, **.bar
foo./ba?/
foo.0:2
foo bar baz
Discussion
Sign in or Join to comment or subscribe
James Harr
2022-04-12T02:05:56Z ago
This is super neat. I’ve used a similar thing in Python for testing called jsonpath, and jsonpath_ng. Both feel a lot like Xpath, but for JSON.
We work on an orchestration system that more or less transforms custom data model inputs into vendor specific data model outputs, and since both inputs and outputs can be viewed as JSON, it has been about the best way to validate some fairly deeply nested properties in a relatively quick way. It’s not perfect, but we get a huge bang for our buck on the tests.
ehmicky
Remote
Back-end lead developer
2022-04-12T14:15:00Z ago
Interesting!
This project is actually directly inspired by JSON paths (which itself is inspired by XPath as you mentioned). I liked the idea and re-used many of their concepts: recursion, slices, unions. However, I found many issues with the JavaScript implementations, mainly: they are very slow and they only allow getting data, not setting it.
I also felt like the syntax itself of JSON paths could be simpler. They also allow consumers to specify imperative logic for both filtering and mapping, which can be a little cumbersome, whereas for most cases, regular expressions might be sufficient. More elaborate use cases can be built on top of the core library instead, which I’ve tried to showcase by providing a small functional utilities library wild-wild-utils with
map()
,include|exclude()
,find()
, etc.If you’re only interested in declarative validation on JSON data, I would also suggest checking out JSON schemas (although I am guessing you might already have), including the great JavaScript implementation ajv. Not everyone is a fan since the syntax can quickly get quite verbose. But they are very expressive, flexible, and widely used.
James Harr
2022-04-12T15:10:16Z ago
JSON schema is probably in our future, though I hate writing it. I might need another tool like CUE which can export JSON schema just so I can avoid hating myself.
We’re not validating the schema in our testing, that’s already rigidly enforced by the orchestrator framework and YANG. We’re actually checking for behavior. IE: when we create a customer facing BGP service with parameters x y and z, on this platform should see an routed interface created with certain encapsulation types and addressing, BGP neighbor relationship created, routing policy created, etc.
The orchestration platform we work with treats the entire network as one Yang tree we can query through RestConf. Sometimes it’s easier just to get the entire configuration for a mock device once and validate the bits and pieces without any extra I/O. That’s where path query tools like this are nice.
All our tests are written in Python though, which excludes WWP unfortunately. It looks nice
James Harr
2022-04-12T02:06:46Z ago
This is super neat. I’ve used a similar thing in Python for testing called jsonpath, and jsonpath_ng. Both feel a lot like Xpath, but for JSON.
We work on an orchestration system that more or less transforms custom data model inputs into vendor specific data model outputs, and since both inputs and outputs can be viewed as JSON, it has been about the best way to validate some fairly deeply nested properties in a relatively quick way. It’s not perfect, but we get a huge bang for our buck on the tests.