Flattening Nested Data Structures

Graeme Stirling
3 min readSep 6, 2022

--

Dealing with nested data can be tricky problem when we want to filter information based on nested data. For example from the data set below we want to create a method which can return “Fruits” or “Vegetables” depending on the item name that is passed as an argument:

Each object in the database is categorised as a “Fruits” or “Vegetables” which has a type and within that type we have many individual items. I find that a tree structure diagram is helpful to easily understand the nested data:

Tree diagram of nested JSON

For us we can intuitively guess whether an item is a fruit of vegetable given its name. Our challenge is to create a method which can receive an item name and return its category (Fruits or Vegetables). The computer however will have to work this out through filtering this nested data structure.

To do this we want to create flattened data that can be easily filtered. Here is an example model of the data in the desired flat structure using a Scala case class:

So we can now create a service/helper which can flatten the data and then filter through it finding the name that matches the given argument. Lets start by defining our functions inputs and outputs:

We use an empty List() on line 8 in order to make sure our code compiles. Our object method is used to transform the data into a structure that can be easily filtered. Like all things in programming an incremental approach is most effective so we will start by flattening this data into just a single layer of nesting:

In this first step we have broken down the first layer of nesting, saving the item category and itemsWithinCategory as variables. We have used foldLeft to deconstruct the first layer of nesting and we can use it again in a similar fashion to deconstruct the second layer saved within our “itemsWithinCategory” variable.

At this point we have used foldLeft to extract and save each piece of information needed to create our flattened data objects “FlatFruitOrVeg”. We can now map over the values to produce our flattened data structure.

With line 16 we can verify our Flattened list of FlatFruitOrVeg is correct. Now that we have achieved the desired outcome we can refactor this method:

Now that we have a method that can flatten the nested data received we can filter to search for an item containing the name searched and return its category:

Perfect ! The “getCategoryByItemName” method can now accept an item’s name as an argument and return the matching category. This allows the computer to determine whether or not it is a fruit or veg via the items name.

I found this method of restructuring nested data extremely interesting and I loved using a simple incremental process to systematically see progress towards the end goal. By printing each step to the console you assert that the output matches the desired outcome at each stage minimising bugs.

Thank you to Ryan Pierre(@ryanpierre) @ Makers for leading a great workshop which inspired this post. Hopefully it is of use to you !

--

--

Graeme Stirling

A Snowsports Coach turned Software developer blogging about my journey/learning. I want to bring understanding to others on complicated software concepts.