Splunk Search

How to split the JSON array into multiple new events?

jhcbazinga95
Loves-to-Learn Everything

Hey all,

Can someone help me out with a JSON related question! Many many thanks! 😄

I have a JSON arrays field in this format

results=<200 OK OK,
{
"tnPortingActivityInProgress" : "N",
"availableActions" :
[
{
"accountAction" : "Restart",
"actionAvailable" : "N",
"actionNotAvailableReason" : "Account is Active"
},
{
"accountAction" : "Multi-AP Salable",
"actionAvailable" : "Y"
},
{
"accountAction" : "Seasonal Suspend",
"actionAvailable" : "Y"
}
],
"transactionId" : "1234567"
}
,[]>

I would love to parse the json array into this format. 

transactionId aaccountAction    actionAvailable  actionNotAvailableReason
1234567 Restart N Account is Active
1234567 Multi-AP Salable Y  
1234567

Seasonal Suspend                  

Y  

 

I have tried a query like this. As you can see, the data is stacked in the same row right now, which is not working in my case  as I have no idea what actionAvailable & actionNotAvailableReason for what accountActions.  And also, the search is not working either if we do like this. 

1.PNG

 

Tags (2)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| eval json=replace(replace(results,"<200 OK OK,",""),",{}>","")
| spath input=json availableActions{} output=availableActions
| mvexpand availableActions
| spath input=availableActions
0 Karma

jhcbazinga95
Loves-to-Learn Everything

Heyyy! 

@ITWhisperer Thank you so much for the helpful reply!🙂 It does separate data into different rows. However, when I bring data from other service/actions,  data is not combined in a expected way. 

Since it is my second week using Splunk, please excuse my native & obvious questions.😅

Below are some detailed explanations & screenshots.

results (where accountAction actionAvailable actionNotAvailableReason are from) is from  action=getAccountAvailableActions

specialtyProduct is from action=getServiceabilityByAddressId_V3

customerType is from "isRepeatTroubleCallEligible" 

Below is the query I used to test. As you can see, either data source will be "null",  and when I search for both of them are not "null", it returns no result.

2.PNG

When I tried to aggregate data a little bit to ensure there are data showing up, it just goes back to the stacked result😢

3.PNG

 

Below is the final query I have came up with. What I eventually want to achieve is a result that I can filter on by playing with customerType, accountAction, smb_ind and actionAvailable to do some negative tests to monitor if the backend logics for different accountActions (such as Multi-AP Salable) are running correct or not. Due to the multiple returned values from JSON array, statistics table is not showing the correct result at this moment. 

For example, for the highlighted transaction_id, actionAvailable returns "Y" in the table, but when I dive into  event. it actually is N when accountAction is Multi-AP Salable. Please see the screenshot I attached in the message.

9.PNG

 

 8.PNG

10.PNG

 

Thank you again for the responsive reply. Looking forward to hearing insights from you very soon!

Cheers!

0 Karma

yuanliu
SplunkTrust
SplunkTrust

@jhcbazinga95 First off, it's much easier to get help if you post sample code in text.

@ITWhisperer is just showing you how to extract fields.  You still need to do stats according to plan.

 

| eval json=replace(replace(results,"<200 OK OK,",""),",{}>","")
| spath input=json path=transactionId
| spath input=json path=availableActions{}
| mvexpand availableActions{}
| spath input=availableActions{}
| stats values(actionNotAvailableReason) as actionNotAvailableReason by transactionId accountAction actionAvailable

 

 Using your sample data, output is

transactionIdaccountActionactionAvailableactionNotAvailableReason
1234567Multi-AP SalableY 
1234567RestartNAccount is Active
1234567Seasonal SuspendY 
0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...