Splunk Search

Using MVZip and MVExpand on MultiValue fields where array sometimes doesnt exists

rajkumarsowmy
New Member

{
"timestamp": "2019-04-11T16:44:45.497462",
"payload": {
"KEY_CHK_DCN_NBR": "19054",
"recommendations": [
{
"modelName": "abc",
"description": "30",
"actionCode": "0261109614",
"actionValue": 0.027422948195084923
},
{
"modelName": "abc",
"description": "30",
"actionCode": "0261109614",
"actionValue": 0.027422948195084923
}
],
"respCd": "700",
}

I have a api logging this information in splunk.
I need to extract
timestamp, payload{}.KEY_CHK_DCN_NBR, payload{}.recommendations.actionCode and payload{}.recommendations.actionvalue

i tried below,

|spath output="DCN Number" path=payload.KEY_CHK_DCN_NBR
|spath output=Timestamp path=timestamp
|spath path=payload.recommendations{} output=r
|mvexpand r
|rename r as _raw
|kv
|rename actionCode ,actionValue
|table "DCN Number" actionCode actionValue Timestamp
| search "DCN Number"!=null

what happens is, in some of the request recommendation array may not be coming, still i need to capture KEY_CHK_DCN_NBR and timestamp and empty value for actioncode and actionvalue.

with my try im able to get all the non-null value.

can anyone help here?

Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

Give this a try

your base search | spath | table payload* timestamp | rename payload.recommendations{}.* as * payload.* as *  | fields - description modelName respCd | eval temp=coalesce(mvzip(actionCode,actionValue,"##"), "") | mvexpand temp | rex field=temp "(?<actionCode>.+)##(?<actionValue>.+)" | fields - temp

See this runanywhere search with sample data

| gentimes start=-1 | eval _raw="{
 \"timestamp\": \"2019-04-11T16:44:45.497462\",
\"payload\": {
 \"KEY_CHK_DCN_NBR\": \"19054\",
 \"recommendations\": [
 {
 \"modelName\": \"abc\",
 \"description\": \"30\",
 \"actionCode\": \"0261109614\",
 \"actionValue\": 0.027422948195084923
 },
 {
 \"modelName\": \"abc\",
 \"description\": \"30\",
 \"actionCode\": \"0261109614\",
 \"actionValue\": 0.027422948195084923
 }
 ],
 \"respCd\": \"700\",
}" | table _raw  | append [| gentimes start=-1 | eval _raw="{
 \"timestamp\": \"2019-04-11T16:44:45.497462\",
\"payload\": {
 \"KEY_CHK_DCN_NBR\": \"19054\",
 \"respCd\": \"700\",
}" | table _raw ] | spath | table payload* timestamp | rename payload.recommendations{}.* as * payload.* as *  | fields - description modelName respCd | eval temp=coalesce(mvzip(actionCode,actionValue,"##"), "") | mvexpand temp | rex field=temp "(?<actionCode>.+)##(?<actionValue>.+)" | fields - temp

View solution in original post

0 Karma

somesoni2
Revered Legend

Give this a try

your base search | spath | table payload* timestamp | rename payload.recommendations{}.* as * payload.* as *  | fields - description modelName respCd | eval temp=coalesce(mvzip(actionCode,actionValue,"##"), "") | mvexpand temp | rex field=temp "(?<actionCode>.+)##(?<actionValue>.+)" | fields - temp

See this runanywhere search with sample data

| gentimes start=-1 | eval _raw="{
 \"timestamp\": \"2019-04-11T16:44:45.497462\",
\"payload\": {
 \"KEY_CHK_DCN_NBR\": \"19054\",
 \"recommendations\": [
 {
 \"modelName\": \"abc\",
 \"description\": \"30\",
 \"actionCode\": \"0261109614\",
 \"actionValue\": 0.027422948195084923
 },
 {
 \"modelName\": \"abc\",
 \"description\": \"30\",
 \"actionCode\": \"0261109614\",
 \"actionValue\": 0.027422948195084923
 }
 ],
 \"respCd\": \"700\",
}" | table _raw  | append [| gentimes start=-1 | eval _raw="{
 \"timestamp\": \"2019-04-11T16:44:45.497462\",
\"payload\": {
 \"KEY_CHK_DCN_NBR\": \"19054\",
 \"respCd\": \"700\",
}" | table _raw ] | spath | table payload* timestamp | rename payload.recommendations{}.* as * payload.* as *  | fields - description modelName respCd | eval temp=coalesce(mvzip(actionCode,actionValue,"##"), "") | mvexpand temp | rex field=temp "(?<actionCode>.+)##(?<actionValue>.+)" | fields - temp
0 Karma

rajkumarsowmy
New Member

Thank you very much @somesoni2 this resolved my issues!!

0 Karma
Get Updates on the Splunk Community!

Splunk Observability Cloud’s AI Assistant in Action Series: Analyzing and ...

This is the second post in our Splunk Observability Cloud’s AI Assistant in Action series, in which we look at ...

Elevate Your Organization with Splunk’s Next Platform Evolution

 Thursday, July 10, 2025  |  11AM PDT / 2PM EDT Whether you're managing complex deployments or looking to ...

Splunk Answers Content Calendar, June Edition

Get ready for this week’s post dedicated to Splunk Dashboards! We're celebrating the power of community by ...