Splunk Search

Extract JSON Data

mchennam
Engager

Need help to find a way to search JSON strings where an attributes is empty.

Ex:  get all JSON data where 'tags' is empty.  

Appreciate your help.

 

{
   "configurationItemCaptureTime":"2021-04-09T00:40:20.800Z",
   "relationships":[
      
   ],
   "configurationStateId":3456,
   "configurationItemVersion":"1.3",
   "tags":{
      
   },
   "awsAccountId":"12345678",
   "supplementaryConfiguration":{
      
   },
   "ARN":"arn:aws:ec2:us-east-1:4567:instance/i-0b8973444340",
   "configurationStateMd5Hash":"",
   "awsRegion":"us-east-1",
   "resourceId":"i-0343434345b26510",
   "configurationItemStatus":"ResourceDeleted",
   "resourceType":"AWS::EC2::Instance",
   "relatedEvents":[
      
   ]
}

 

 

 

 

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Apart from trawling through all your data (I am not sure you can avoid that), your search relies on the empty tags element having nothing between the braces - is this guaranteed to be always the case? If not, perhaps you could consider the regex command to filter the events with match a regular expression that copes with variations. Something like this perhaps?

| regex _raw="\"tags\":\s*\{[\s]*\}"

 

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

You could use rex to edit the JSON to include a known (and previously unused) field with a known (and previously unused) value, then collect all the field names that match these strings

| makeresults 
| eval _raw="{
   \"configurationItemCaptureTime\":\"2021-04-09T00:40:20.800Z\",
   \"relationships\":[
      
   ],
   \"configurationStateId\":3456,
   \"configurationItemVersion\":\"1.3\",
   \"tags\":{
      
   },
   \"awsAccountId\":\"12345678\",
   \"supplementaryConfiguration\":{
      
   },
   \"ARN\":\"arn:aws:ec2:us-east-1:4567:instance/i-0b8973444340\",
   \"configurationStateMd5Hash\":\"\",
   \"awsRegion\":\"us-east-1\",
   \"resourceId\":\"i-0343434345b26510\",
   \"configurationItemStatus\":\"ResourceDeleted\",
   \"resourceType\":\"AWS::EC2::Instance\",
   \"relatedEvents\":[
      
   ]
}"


| rex mode=sed "s/\{[^\w]*\}/{\"field\":\"not available\"}/g"
| rex mode=sed "s/\[[^\w]*\]/[{\"field\":\"not available\"}]/g"
| spath
| foreach *
    [ eval emptyattributes=if("<<FIELD>>"!="emptyattributes",if('<<FIELD>>'=="not available",if(isnull(emptyattributes),mvindex(split("<<FIELD>>",".field"),0),mvappend(emptyattributes,mvindex(split("<<FIELD>>",".field"),0))),emptyattributes),emptyattributes)
    ]
| fields emptyattributes
0 Karma

mchennam
Engager

Thanks for your time. I am sorry for not being clear on what i need. The log file contains json strings. I am trying to identify the rows containing empty 'tags:{}'  attribute.  And then show the extracted fields in a table.

The following is what i have come up with so far.  Not sure if this is a efficient way to query thousand of records.

 index=xxx sourcetype="aws:config"  "\"tags\": {}"  | table ARN, aws_account_id, awsRegion, availabilityZone, resourceId, resourceCreationTime, configurationItemStatus, configurationItemCaptureTime

 

ARN

aws_account_id

awsRegion

availabilityZone

resourceId

resourceCreationTime

configurationItemStatus

configurationItemCaptureTime

arn:aws:ec2:us-east-1:23233232:instance/i-02d7ce4ac0daa3a94

23232323

us-east-1

us-east-1a

i-232323232323

2021-04-09T13:00:16.000Z

ResourceDiscovered

2021-04-09T13:01:58.577Z

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Apart from trawling through all your data (I am not sure you can avoid that), your search relies on the empty tags element having nothing between the braces - is this guaranteed to be always the case? If not, perhaps you could consider the regex command to filter the events with match a regular expression that copes with variations. Something like this perhaps?

| regex _raw="\"tags\":\s*\{[\s]*\}"

 

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Quick connection discovery mode for forwarders

When a Splunk forwarder loses connectivity to its indexers, it does not always reconnect immediately. In many ...

Build and Launch AI Agents from Your Splunk Workflows

  Register We’ve all been there: juggling alerts, runbooks, and endless manual searches. What if you could ...

Splunk Cloud Application Management in Terraform

Register   On Tuesday, August 4 at 11AM PDT / 2PM EDT, we’re diving into how you can bring Infrastructure as ...