Splunk Search

Extract the values from an array of objects

AshimaE
Explorer

Using rex a field has been extracted which has a format of an array with multiple elements of the type,

[{"name":"planning","confidence":0.98},{"name":"sales","confidence":0.12}...]

So basically it is an array of objects of type

{"name":"planning","confidence":0.98}

I need to iterate this array and extract all values of name and confidence to check for certain conditions. Unable to extract it properly. Tried using split and trim among many other things. could someone suggest how to extract each object one by one and do the necessary checks and computations on the same.

0 Karma
1 Solution

harshpatel
Contributor

Hi @AshimaE,

try this on your data (please ignore till | eval _raw="[{\"name\":\"planning\",\"confidence\":0.98},{\"name\":\"sales\",\"confidence\":0.12}]") :

| makeresults count=1 
| eval _raw="[{\"name\":\"planning\",\"confidence\":0.98},{\"name\":\"sales\",\"confidence\":0.12}]" 
| spath 
| eval all_fields=mvzip('{}.name', '{}.confidence', ",") 
| fields all_fields 
| mvexpand all_fields 
| makemv delim="," all_fields 
| eval name=mvindex(all_fields, 0) 
| eval confidence=mvindex(all_fields, 1) 
| table name, confidence

This will process your JSON array to table in Splunk which will be easy to process later on. If you have all of your events in one single event as JSON array then I would recommend splitting it into one single JSON object and ingest. Because parsing at search will reduce the performance of your search.

Hope this helps 🙂

View solution in original post

harshpatel
Contributor

Hi @AshimaE,

try this on your data (please ignore till | eval _raw="[{\"name\":\"planning\",\"confidence\":0.98},{\"name\":\"sales\",\"confidence\":0.12}]") :

| makeresults count=1 
| eval _raw="[{\"name\":\"planning\",\"confidence\":0.98},{\"name\":\"sales\",\"confidence\":0.12}]" 
| spath 
| eval all_fields=mvzip('{}.name', '{}.confidence', ",") 
| fields all_fields 
| mvexpand all_fields 
| makemv delim="," all_fields 
| eval name=mvindex(all_fields, 0) 
| eval confidence=mvindex(all_fields, 1) 
| table name, confidence

This will process your JSON array to table in Splunk which will be easy to process later on. If you have all of your events in one single event as JSON array then I would recommend splitting it into one single JSON object and ingest. Because parsing at search will reduce the performance of your search.

Hope this helps 🙂

Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...