JSONata Expressions โœจ

Transform data like a wizard! JSONata expressions are your magic wand for accessing, transforming, and combining data in your workflows.

Why JSONata? ๐Ÿค”

JSONata makes data transformation fun and powerful:

  • ๐ŸŽฏ Access any data with simple dot notation
  • ๐Ÿ”„ Transform arrays and objects like a pro
  • ๐Ÿง™โ€โ™‚๏ธ Write expressions that are actually readable
  • ๐ŸŽจ Shape your data exactly how you want it
  • ๐Ÿš€ Do more with less code

The Basics โœจ

Getting data is super easy:

// Given this data:
{
  "user": {
    "name": "Alice",
    "email": "alice@example.com",
    "orders": [
      { "id": "123", "total": 50 }
    ]
  }
}

// These expressions:
user.name              // "Alice"
user.orders.id         // ["123"]
user.orders.total      // [50]

Fun with Arrays ๐ŸŽช

Arrays are your playground:

// Given this data:
{
  "items": [
    { "name": "Laptop", "price": 1200, "category": "Electronics" },
    { "name": "Book", "price": 20, "category": "Books" },
    { "name": "Phone", "price": 800, "category": "Electronics" }
  ]
}

// These expressions:
items.name                     // ["Laptop", "Book", "Phone"]
items[price > 1000].name      // ["Laptop"]
$sum(items.price)             // 2020
items.{                       // Transform each item
  "id": name,
  "cost": price * 1.2,       // 20% markup
  "type": category
}

Magic Tricks ๐ŸŽฉ

Here are some cool things you can do:

Combining Data ๐Ÿคน

// Given this data:
{
  "user": { "name": "Alice", "email": "alice@example.com" },
  "orders": [
    { "id": "123", "items": ["book", "pen"], "total": 25 }
  ]
}

// This expression:
{
  "customer": user.name,
  "email": user.email,
  "orderCount": $count(orders),
  "totalSpent": $sum(orders.total)
}

Smart Conditions ๐ŸŽฒ

// Time-based greeting
$case(
  $hour() < 12 ? 'Good morning',
  $hour() < 17 ? 'Good afternoon',
  'Good evening'
)

// Conditional pricing
amount > 100 ? amount * 0.9 : amount  // 10% discount over 100

Data Cleanup ๐Ÿงน

// Given this data:
{
  "tags": ["js", null, "typescript", "", "react", null],
  "categories": ["books", "electronics", "books", "games"]
}

// These expressions:
tags[$ != null and $ != ""]    // Remove empty values
$distinct(categories)          // Get unique values

Using JSONata in Workflows ๐Ÿ”„

In our workflows, JSONata powers dynamic data resolution in several places:

URL Parameters ๐Ÿ”—

{
  url: "https://api.example.com/users/:userId",
  urlParams: {
    userId: {
      jsonataExpression: "user.id"  // Resolves to the actual user ID
    }
  }
}

Query Parameters ๐Ÿ”

{
  queryParams: {
    status: {
      jsonataExpression: "order.status"
    },
    limit: {
      jsonataExpression: "$number(pagination.limit)"
    }
  }
}

Request Bodies ๐Ÿ“ฆ

{
  body: {
    jsonataExpression: `{
      "userId": user.id,
      "items": cart.items.{
        "productId": id,
        "quantity": quantity
      },
      "total": $sum(cart.items.(price * quantity))
    }`
  }
}

Pro Tips ๐Ÿ’ซ

Make your expressions awesome:

  • ๐ŸŽฏ Start simple, add complexity as needed
  • ๐Ÿ” Use the JSONata playground to test
  • ๐Ÿ“ Break complex expressions into parts
  • โœจ Remember: readability > cleverness
  • ๐ŸŽจ Format your expressions nicely

Ready to transform some data? Let's go! ๐Ÿš€