JSONata Playground

Apr 14, 2022

Products

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.

We love JSONata. It's a modest little language. Where the likes of Javascript or Python try to be all things to all people, JSONata just tries to do one thing very well: query JSON structures. That's not something every developer runs into every day, but it's exactly what we need for Mappings, which is all about transforming one JSON structure into another.

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

It works, but we can do better. We took a few days and created the JSONata Playground. In the playground, it's clear what all of these snippets are, and it's interactive, so you can play around with the JSONata expression and discover how it was constructed. You can also embed the playground in a webpage.

You can bet that from now on our StackOverflow answers will include JSONata Playground examples and yours can as well, because it's completely free for anyone. We hope you'll use it to learn JSONata, to write JSONata, and to share JSONata. We hope this helps you out. And we hope we impressed you a little bit, too.

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.

We love JSONata. It's a modest little language. Where the likes of Javascript or Python try to be all things to all people, JSONata just tries to do one thing very well: query JSON structures. That's not something every developer runs into every day, but it's exactly what we need for Mappings, which is all about transforming one JSON structure into another.

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

It works, but we can do better. We took a few days and created the JSONata Playground. In the playground, it's clear what all of these snippets are, and it's interactive, so you can play around with the JSONata expression and discover how it was constructed. You can also embed the playground in a webpage.

You can bet that from now on our StackOverflow answers will include JSONata Playground examples and yours can as well, because it's completely free for anyone. We hope you'll use it to learn JSONata, to write JSONata, and to share JSONata. We hope this helps you out. And we hope we impressed you a little bit, too.

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.

We love JSONata. It's a modest little language. Where the likes of Javascript or Python try to be all things to all people, JSONata just tries to do one thing very well: query JSON structures. That's not something every developer runs into every day, but it's exactly what we need for Mappings, which is all about transforming one JSON structure into another.

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

It works, but we can do better. We took a few days and created the JSONata Playground. In the playground, it's clear what all of these snippets are, and it's interactive, so you can play around with the JSONata expression and discover how it was constructed. You can also embed the playground in a webpage.

You can bet that from now on our StackOverflow answers will include JSONata Playground examples and yours can as well, because it's completely free for anyone. We hope you'll use it to learn JSONata, to write JSONata, and to share JSONata. We hope this helps you out. And we hope we impressed you a little bit, too.

Share

Twitter
LinkedIn

Backed by

Stedi is a registered trademark of Stedi, Inc. All names, logos, and brands of third parties listed on our site are trademarks of their respective owners (including “X12”, which is a trademark of X12 Incorporated). Stedi, Inc. and its products and services are not endorsed by, sponsored by, or affiliated with these third parties. Our use of these names, logos, and brands is for identification purposes only, and does not imply any such endorsement, sponsorship, or affiliation.

Backed by

Stedi is a registered trademark of Stedi, Inc. All names, logos, and brands of third parties listed on our site are trademarks of their respective owners (including “X12”, which is a trademark of X12 Incorporated). Stedi, Inc. and its products and services are not endorsed by, sponsored by, or affiliated with these third parties. Our use of these names, logos, and brands is for identification purposes only, and does not imply any such endorsement, sponsorship, or affiliation.

Backed by

Stedi is a registered trademark of Stedi, Inc. All names, logos, and brands of third parties listed on our site are trademarks of their respective owners (including “X12”, which is a trademark of X12 Incorporated). Stedi, Inc. and its products and services are not endorsed by, sponsored by, or affiliated with these third parties. Our use of these names, logos, and brands is for identification purposes only, and does not imply any such endorsement, sponsorship, or affiliation.