Skip to content

TransformationExample

Glythcing edited this page Sep 17, 2018 · 3 revisions

The following example shows the use of aliases and literals to transform an input. Tranquil can transform an input using:

  • A subset of the attributes in the input
  • Additional attributes which were not present in the input. These additional attributes may have:
    • Literal values
    • Derived values from string concatenation
    • Derived values from simple arithmetic operations
  • Attributes which were in the input but have been renamed
Given
[
  {
    "name": "tap",
    "price": 49.99,
    "quantity": 10,
    "active": true,
    "owner": null,
    "since": "2018-09-07"
  },
  {
    "name": "sink",
    "price": 99.99,
    "quantity": 100,
    "active": false,
    "owner": null,
    "since": "2018-09-02"
  }
]
When
Tranquil.parse(json).read(
  "name as pseudonym, 'gold plated ' + name as tacky, 0 as outOfStockQuantity, 2 * price as doublePrice", 
  "quantity = 10"
)
Then

Tranquil will return:

{
  "pseudonym": "tap",
  "tacky": "gold plated tap",
  "outOfStockQuantity": 0,
  "doublePrice": 99.98
}
Clone this wiki locally