Parsing complex JSON structures is usually not a trivial task. When your destination is a database, what you expect naturally is a flattened result set. Things get more complicated when your JSON source is a web service and the result consists of multiple nested objects including lists in lists and so on. Things get even more complicated if the JSON schema changes over time, which is often a real-life scenario.
We have these wonderful Azure Logic Apps, which help us consistently get the JSON results from various sources. However, Logic Apps are not so good at parsing more complex nested structures. And they definitely don’t like even subtle source schema changes.
With Databricks you get:
- An easy way to infer the JSON schema and avoid creating it manually
- Subtle changes in the JSON schema won’t break things
- The ability to explode nested lists into rows in a very easy way (see the Notebook below)
Following is an example Databricks Notebook (Python) demonstrating the above claims. The JSON sample consists of an imaginary JSON result set, which contains a list of car models within a list of car vendors within a list of people. We want to flatten this result into a dataframe. Here you go:
We've seen here how we can use Databricks to flatten JSON with just a few lines of code.
Keep your eyes open for future Databricks related blogs, which will demonstrate more of the versatility of this great platform.
More on some of the used functions (PySpark 2.3.0 documentation):