A Flat Table with Child Records
The Problem
Our input data from the Flat Table example has been extended to add an address record per employee:
[
{
"name": "Robert Pringles",
"email": "bob@pringles.pop",
"address": {
"planet": "Earth"
}
},
{
"name": "Daniel Sausage",
"email": "danny@pringles.pop",
"address": {
"planet": "Mars"
}
},
{
"name": "Charlie Marmalade",
"email": "charlie@pringles.pop",
"address": {
"planet": "Pluto"
}
}
]
To add a Planet column to our table, we’ll describe a path that drills into this new child record.
Code Sample
This code is similar to the Flat Table example except for a new Planet column, which uses dots to describe a path into the address child record:
from rolumns import Columns
from rolumns.renderers import RowsRenderer
data = [
{
"name": "Robert Pringles",
"email": "bob@pringles.pop",
"address": {
"planet": "Earth"
}
},
{
"name": "Daniel Sausage",
"email": "danny@pringles.pop",
"address": {
"planet": "Mars"
}
},
{
"name": "Charlie Marmalade",
"email": "charlie@pringles.pop",
"address": {
"planet": "Pluto"
}
},
]
columns = Columns()
columns.add("Name", "name")
columns.add("Email", "email")
columns.add("Planet", "address.planet")
renderer = RowsRenderer(columns)
rows = renderer.render(data)
print(list(rows))
Result
[['Name', 'Email', 'Planet'],
['Robert Pringles', 'bob@pringles.pop', 'Earth'],
['Daniel Sausage', 'danny@pringles.pop', 'Mars'],
['Charlie Marmalade', 'charlie@pringles.pop', 'Pluto']]