Data Flattening Overview

When configuring a connector endpoint that is supposed to receive data, you want this to be flattened into a tabular format that can be easily stored into a relational database.

For this purpose, hibase provides a data manipulation syntax made of path selectors, the goal of which is to select data at any depth and assign it to a column for tabular representation.

Under the hood, the result of flattening a piece of data is a set of columns and rows of the same length. As an illustratory example, the flat representation of a piece of JSON looking like this...

[
    {
        "id": 1,
        "name": "Red"
    },
    {
        "id": 2,
        "name": "Blue"
    }
]

...might be look like this once processed by hibase:

{
    "headers": ["id", "name"],
    "data": [
        [1, "Red"],
        [2, "Blue"]
    ]
}

Root paths and path types

In principle, hibase takes an opinionated approach while parsing data returned by any service, in that it expects parts of the response to represent data or rows, other parts to potentially represent headers or columns and a third portion of the data to represent metadata, possibly useful for things like pagination.

  • Data paths are required ones in any endpoint that is supposed to return data
  • Column paths can be provided optionally, in case columns information is returned by the HTTP response separately from data
  • Metadata is also optional and is the only one of the three which is kept in memory once encountered and parsed, in order to be made available to subsequent HTTP calls

As an illustratory example, imagine this response being returned by the service you are consuming:

{
    "attributes": {
        "dimensions": ["date", "country"],
        "metrics": ["clicks", "orders"]
    },
    "results": [
        ["2019-23-11", "DE", 123, 10],
        ["2019-23-12", "DE", 200, 15],
        // ...
    ],
    "status": {
        "total": 1242,
        "currentPage": 1,
        "nextPage": true
    }
}

In such a case, your Root data path would be results[*], your Root columns path attributes and your Metadata path status. You can then configure so-called subpaths to snatch the pieces of data that you care about within each of the three domains.

Data paths

Within the context of parsing an HTTP response within an endpoint, Data paths are selectors (or groups thereof) which define the following settings for the parsing to happen:

Subpath

An expression representing a selector relatively to the root path. For example, given this piece of data:

    {
        "records": [
            {"id": 1, "name": "Red"},
            {"id": 2, "name": "Blue"}
        ]
    }

... your Root data path could very well be records[*] and viable subpaths are id and name.

Transform

It's possible to transform pieces of data after they have been selected. Be wary that this could be an expensive thing to do, especially if you want to perform complex transformations onto large data sets. A better solution for these scenarios would be to take an ELT approach and perform your transformations after having loaded the data into your database.

Transform accepts a parametric expression receiving the data selected by the Subpath selector as {{ $element }}.

In the example above, you could have the following Transform value attached to your id subpath:

{{ $element + 100 }}

which would produce IDs 101 and 201 in your output. See the hibase syntax docs for a full overview of what is possible through parametric expressions.

Column names

A comma separated list of header names to assign to your data, useful when the structure of the data selected by your Subpath is fixed or when headers information is missing.

Column types

A list of types to be assigned to each column name specified above. The number of types must match the number of column names if higher than one. Alternatively, a single type specified in the field will be applied to all columns selected by the Subpath.

Column path

An expression representing a selector relatively to the root data path. Useful when you are selecting header names right out of the data, for example here:

    {
        "records": [
            {
                "id": 1, 
                "attributes": [
                    {
                        "key": "name",
                        "value": "Blue"
                    },
                    {
                        "key": "description",
                        "value": "The blue color"
                    }
                ]
            },
            // ...
        ]
    }

Given your Root data path at records[*], you could set up one of your Data paths as follows:

  • Subpath: attributes[*].value
  • Columns path: attributes[*].key

Note that Columns path operates in the same context as its Subpath, simply pointing at another place in the data.

Transform columns

When Columns path is defined, you also have the option to perform transformations onto its output. The Transform columns field is provided for this purpose, and its value must be a parametric JSON expression with keys name and column_type, receiving the selected value as $element.

In the example above, setting the Transform columns field to the following:

{
    "name": "color_{{ $element }}",
    "column_type": "string"
}

... would produce the following columns:

[
    {
        "name": "color_name",
        "column_type": "string"
    },
    {
        "name": "color_description",
        "column_type": "string"
    }
]

Condition

It's possible to specify parametric conditions based on which a Data path should be enabled or not while parsing an HTTP response. The Condition field is provided for this purpose and it accepts a parametric expression that should evaluate to either true or false.

The parametric expression here has access to $endpoint_params, allowing you to configure optional fields based on the user's input.

For example, given an Endpoint parameter of type Toggle and with label include_attributes, you could attach it to functionality that enables / disable your path in the example above by using the condition:

{{ $endpoint_params.include_attributes }}

Data flattening syntax

Selectors have their own syntax to let you point to, iterate over and pivot pieces of data that are returned at different locations and within different data structures.

These different notations can be chained and combined freely to achieve powerful manipulations of your data.

Attribute selectors

Given any JSON object in your data, you can select values by referencing keys with a dot notation. For example, while operating on the following piece of JSON:

{
    "attributes": {
        "id": 1,
        "name": "Blue"
    }
}

... these are valid selectors:

attributes.id
attributes.name

As a shorthand notation, keys within a common root can be referenced by separating them with commas. The notation above is thereby equivalent to:

attributes.id,name

Array index selectors

Much like attribute selectors, array indices allow you to select a given element within an array. Their notation is the index you want to select (starting at 0) wrapped into square brackets, for example [0].

Given the following piece of JSON:

{
    "id": 1,
    "variants": [
        {"size": "L", "color": "blue"},
        {"size": "XL", "color": "red"}
    ]
}

... you can select just the first of the variants' attributes as follows:

variants[0].size,color

Iterating over arrays

Whenever you have a collection of records in your data, you can iterate over it with a [*] notation. For example, given the following piece of JSON:

{
    "id": 1,
    "variants": [
        {"size": "L", "color": "blue"},
        {"size": "XL", "color": "red"}
    ]
}

... you can iterate over variants through the following selectors:

variants[*].size
variants[*].color

This will produce one row per variant in the example above:

size color
L blue
XL red

Note that Attribute selectors coming after the [*] notation refer to every object within the array.

Generic Key/Value selectors

Whenever you are dealing with non-fixed data structures, you want some generic selectors to do the flattening work for you without having to list every possible attribute name that might be encountered in the data.

hibase provides two such generic selectors, respectively pointing at the keys and the values of a JSON object: [k*] and [v*].

You can imagine these two selectors as turning an object into an array of its keys or values, and then behaving like the [*] notation described above.

For example, given the following JSON:

{
    "id": 1,
    "variants": [
        {"size": "L", "color": "blue"},
        {"size": "XL", "color": "red", "active": false}
    ]
}

... you can iterate over variants' keys and values as follows:

variants[*][k*]
variants[*][v*]

This will produce as many rows as keys and values combinations in the object:

col1 col2
size L
color blue
size XL
color red
active false

Pivot selectors

As you can see in the example above, exploding all key-value combinations of an object is not always ideal to get a good overview of the data.

In these and other cases, including for example fixed-size arrays describing a known set of attributes, you might want to switch to a columnar representation of the data - what we call pivoting the data.

hibase offers three pivot selectors which mirror the iterators described so far: [**], [k**] and [v**].

These behave as their iterating counterparts, but they pivot their result into columns as an additional step.

Pivoting arrays

Imagine the following piece of JSON:

{
    "id": 1,
    "variants": [
        {"attributes": ["L", "blue"]},
        {"attributes": ["XL", "red"]}
    ]
}

You can achieve one column per attribute in your data by using the [**] notation, as shown in the following selector:

variants[*].attributes[**]

This will produce the following flat representation of the data:

col1 col2
L blue
XL red

Pivoting keys and values

Much like in the last example, you can use the [k**] and / or [v**] notations to extract and pivot keys and values from JSON objects.

Back to our original key-value example:

{
    "id": 1,
    "variants": [
        {"size": "L", "color": "blue"},
        {"size": "XL", "color": "red", "active": false}
    ]
}

You can imagine using the following selectors:

variants[*][k**]
variants[*][v**]

These will produce a flat representation of the data that expands horizontally as keys and values are encountered:

col1 col2 col3
size color
L blue
size color active
XL red false

These selectors are particularly useful in combination to one another, within a single Data path. You can in fact use the [k**] selector as Columns path and the [v**] selector as Subpath to achieve a full flattened representation of any object, with its keys as headers and its values as rows.

Assigning the two selectors to the right fields, this would produce the following in your data:

size color active
L blue
XL red false

Filtering values

All Key/Value selectors, including pivot ones, support the exclusion of a comma-separated list of keys through the following syntax:

[k*-(key1, key2, ..., keyN)]
[v*-(key1, key2, ..., keyN)]
[k**-(key1, key2, ..., keyN)]
[v**-(key1, key2, ..., keyN)]
[kv**-(key1, key2, ..., keyN)]

For example, given the following piece of JSON:

{
    "id": 1,
    "variants": [
        {"size": "L", "color": "blue", "promo": true},
        {"size": "XL", "color": "red", "promo": false, "active": false}
    ]
}

The following selector would lead to the output below:

variants[*][v**-(promo, active)]
col1 col2
L blue
XL red

Combining selectors

As stated above, selectors can be combined freely in order to achieve powerful manipulations of your data.

Imagine this more complex example:

{
    "data": [
        {
            "id": 1,
            "name": "Chair",
            "variants": [
                {
                    "variant_id": "chairB",
                    "attributes": {
                        "price": 100.00,
                        "color": "blue"
                    }
                },
                {
                    "variant_d": "chairR",
                    "attributes": {
                        "price": 90.00,
                        "color": "red",
                        "active": false
                    }
                }
            ]
        },
        {
            "id": 2,
            "name": "Table",
            "variants": [
                {
                    "variant_id": "tableY",
                    "attributes": {
                        "price": 1000.00,
                        "color": "yellow"
                    }
                },
                {
                    "variant_id": "tableG",
                    "attributes": {
                        "price": 900.00,
                        "color": "green"
                    }
                }
            ]
        },
        // ...
    ]
}

You could imagine using the following selectors to get a good flat view of your data:

data[*].id,name
data[*].variants[*].variant_id
data[*].variants[*].attributes[v**]

This will produce the following:

id name variant_id col1 col2 col3
1 Chair chairB 100.00 blue
1 Chair chairR 90.00 red false
2 Table tableY 1000.00 yellow
2 Table tableG 900.00 green

There is no limit as to how deeply you can chain selectors to one another.