JSONata Playground
When a question about JSONata turns up on StackOverflow, we do our best to answer it. Not because it's part of our strategy, or because we are building our corporate image, but because we like to help out. And we like to impress a little bit, too.
Since we do use JSONata every day, we're building up quite a bit of expertise with it. We like building complex queries, like: does the total cost of all items in the order that are not in stock exceed our limit? However, if you only use JSONata occasionally, then a challenge like this may be a bit daunting. JSONata isn't a hard language to learn, but you can do some pretty involved stuff with it. You could dig through the documentation and it does cover all aspects of the language, but it doesn't offer much guidance on how to put all the pieces together. No, a situation like this is when you turn to StackOverflow.
Whenever we post an answer, we keep in mind that the easiest way to understand JSONata is by example, but giving an example isn’t as convenient as we’d like. There are three parts to it: the data you’re querying, the JSONata query, and the result. You can show all of that in code snippets, of course, but it lacks a bit of clarity and it’s very static.
{
"orders": [
{
"quantity": 650,
"pricePerUnit": 3.53,
"inStock": true
},
{
"quantity": 800,
"pricePerUnit": 1.12,
"inStock": false
},
{
"quantity": 3,
"pricePerUnit": 275.75,
"inStock": false
},
{
"quantity": 1250,
"pricePerUnit": 2.45,
"inStock": true
}
]
}
(
$costs := $map(orders[inStock=false], function ($order) {
$order.quantity * $order.pricePerUnit
});
$costs ~> $sum > 1000
)
true
Get familiar with JSONata syntax in Stedi’s playground where you can quickly edit, share, and embed examples.
Get blog posts delivered to your inbox.