Splunk Search

Count distinct instances of a field which is an arbitrary structure

ojensen
Explorer

Say I have events of the form:

{
something: "cool",
subfield: {
this: "may contain",
arbitrary: ["things"],
and: {
more: "stuff"
}
}
}

The internal structure of `subfield` is arbitrary. I would like to count how many different `subfield` values I have. How can I accomplish this?

My initial thought was maybe there was some function I could use to JSON encode the field, so that I could just have an

| eval subfieldstr = to_json_string(subfield) 

and then I could just do a "stats dc" on subfieldstr, but I can't find such a function, and searching for it is difficult (there are endless results of people trying to do the exact opposite)

Labels (3)
0 Karma
1 Solution

ojensen
Explorer

After a lot of experimentation, I've found that I can convert a field into a json-encoded string by simply extracting it from _raw, since json_extract does not seem to operate recursively. It's a bit of a roundabout way of getting there, but it seems to do the trick. So essentially I can do

index=whatever my search here
| eval subfieldstr = json_extract(_raw, "subfield")
| stats dc(subfieldstr) as count

 

View solution in original post

0 Karma

ojensen
Explorer

After a lot of experimentation, I've found that I can convert a field into a json-encoded string by simply extracting it from _raw, since json_extract does not seem to operate recursively. It's a bit of a roundabout way of getting there, but it seems to do the trick. So essentially I can do

index=whatever my search here
| eval subfieldstr = json_extract(_raw, "subfield")
| stats dc(subfieldstr) as count

 

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...