Screenshot 2019-08-10 11.49.13

How to flatten nested events

Ledion Bitincka Headshot

Last edited: February 13, 2020

In this quick How To post we’ll cover how to flatten a nested event, either entirely or just parts of it. We’ll cover two flavors of flattening:

  1. Creating top level fields with fully qualified names, e.g {foo: {bar: 42}} -> {foo_bar: 42}

  2. Promoting a nested object’s field to top level, e.g. {foo: {bar: 42}} -> {bar: 42}

Let’s assume that we’re working with an event that looks like this (internal LogStream log) and we want to flatten or promote the contents of opts

event-to-flatten

Flattening

We recommend using the flatten function which is purpose built for this use-case. It allows you to specify an optional prefix as well as what delimiter to use when creating fully qualified field names. For many destination systems it is challenging to use fields containing a . thus the default delimiter is set to _

flattened

Promoting

We recommend using the Eval function in combination with JavaScript’s Object.assign function to copy fields from the nested object to the top level object, then finally remove the nested object. In this case we’re referencing a special field called __e which references the entire event.  We’re effectively copying fields from opts to the top level, then deleting opts field. You will notice that this method does not recursively promote nested objects (notice metricStorage), you’d have to manually do that, using another statement in the eval.

promote-1

 If you’ve enjoyed reading this far please take Cribl LogStream for a spin and join our community Slack  where lot’s more cool use-cases like this come up frequently.

Cribl, the Data Engine for IT and Security, empowers organizations to transform their data strategy. Customers use Cribl’s suite of products to collect, process, route, and analyze all IT and security data, delivering the flexibility, choice, and control required to adapt to their ever-changing needs.

We offer free training, certifications, and a free tier across our products. Our community Slack features Cribl engineers, partners, and customers who can answer your questions as you get started and continue to build and evolve. We also offer a variety of hands-on Sandboxes for those interested in how companies globally leverage our products for their data challenges.

More from the blog

get started

Choose how to get started

See

Cribl

See demos by use case, by yourself or with one of our team.

Try

Cribl

Get hands-on with a Sandbox or guided Cloud Trial.

Free

Cribl

Process up to 1TB/day, no license required.