Skip to content

NestedExample

Glythcing edited this page Sep 17, 2018 · 2 revisions

The following example shows the use of the projections and predicates against nested attributes. Examples of "nested" are:

  • A sub document within JSON
  • A Map inside a Map
  • An array of sub documents within JSON
  • A collection of Maps inside a Map

The following example is somewhat contrived but it should suffice to show:

  • You query arrays using either
    • A wildcard - [*] - which queries any element in the array
    • An index - [n] - which queries the specific element in the array
  • You query and select nested documents using a . delimited path
  • You project on specific array elements using the element index; [n]
Given
{
  "type": "catalog",
  "status": "DRAFT",
  "version": 3,
  "owner": {
    "name": "glytching",
    "role": "maintainer"
  },
  "items": [
    {
      "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(
  "type, owner.name, items[0].name as firstItemName, items[1].quantity as secondItemQuantity", 
  "type = 'catalog' and owner.role is not null and (items[*].name='sink' or items[0].name='tap')"
)
Then

Tranquil will return:

{
  "type": "catalog",
  "owner.name": "glytching",
  "firstItemName": "tap",
  "secondItemQuantity": 100
}
Clone this wiki locally