draft-ietf-jsonpath-base
draft-ietf-jsonpath-base copied to clipboard
field creation
An interesting question was asked on StackOverflow, and subsequently on my lib's repo, which I thought would be a good suggestion for the V2 spec (supposing that happens).
Proposal
Given the JSON document
{
"options": [
{
"price": 217,
"quantity": 2
},
{
"price": 63,
"quantity": 5
}
]
}
This proposal enables performing a calculation, e.g. price * quantity
, and getting its result by assigning a new property in the local JSON so that it can be queried by subsequence segments.
A path that could do this would be:
$.options[?(@.result = @.price * @.quantity)].result
In this path, @.result
is assigned the result of the multiplication. This assignment would operate as an "upsert" where it would overwrite any value that may already be there. The new value is subsequently returned by the next segment .result
.
This likely has applications beyond this simple example and is worth considering. Additionally, many other query languages (e.g. SQL) permit the creation and selection of temporary values like this.
It's understood that filter expressions as currently defined are required to return a boolean value, and the above expression does not, so that aspect of filter expressions will need to be revisited/addressed.
Depending on how the conversation goes here, I could probably work it up in my implementation as an optional behavior to make it available on my playground.
Caveat
This feature is predicated on math operations being supported, which the base spec does not. Currently we can't even select based on calculated values meeting some threshold, e.g.
$.options[?(@.price * @.quantity > 400)]
Note that these math operators (not assignment, though) are already optionally supported on my playground.