Splunk Dev

Splunkjs - access field names from search data object.

peamc
Explorer

Hoping I'll be able to find an answer to this; any help is much appreciated.

I'm running a search and then populating a custom chart made with Google Charts.

I have no problems getting the results back from a search (as below), but is there a way of returning the field names from the search ? The reason for this is so I can apply these as axis titles (rather than manually set them). Hopefully this makes sense.

var mySearch = splunkjs.mvc.Components.getInstance("search3");
var datasource = mySearch.data('results', {count:0});
console.log("Rows: ", datasource.data().rows);

Tags (2)
0 Karma
1 Solution

tom_frotscher
Builder

Hi,

if you console.log(datasource.data()), you will see an array that contains the fields. So you can use this to also print out the fields:

var mySearch = splunkjs.mvc.Components.getInstance("search3");
var datasource = mySearch.data('results', {count:0});
console.log("Rows: ", datasource.data().rows); 
console.log("Fields: ", datasource.data().fields); 

Greetings

Tom

View solution in original post

tom_frotscher
Builder

Hi,

if you console.log(datasource.data()), you will see an array that contains the fields. So you can use this to also print out the fields:

var mySearch = splunkjs.mvc.Components.getInstance("search3");
var datasource = mySearch.data('results', {count:0});
console.log("Rows: ", datasource.data().rows); 
console.log("Fields: ", datasource.data().fields); 

Greetings

Tom

japposadas
Explorer

i tried using this to retrieve field header names so that i can put it on the a dropdown list. but i always keep on getting Uncaught TypeError: Cannot read property 'rows' of undefined, which ID should I use? the ID for the search bar or the ID of the search manager? or am I lacking on js libraries?

here's my script

require([
"splunkjs/mvc/searchmanager",
"splunkjs/mvc/searchbarview",
"splunkjs/mvc/searchcontrolsview",
"splunkjs/mvc/timelineview",
"splunkjs/mvc/tableview",
"splunkjs/mvc/simplexml/ready!"
], function(
SearchManager,
SearchbarView,
SearchControlsView,
TimelineView,
TableView
) {

// Create the search manager
var mysearch = new SearchManager({
    id: "search1",
    app: "search",
    preview: true,
    cache: true,
    status_buckets: 300,
    required_field_list: "*",
    search: "index=_internal | head 100"
});

// Create the views
var mytimeline = new TimelineView ({
    id: "timeline1",
    managerid: "search1",
    el: $("#mytimeline1")
}).render();

var mysearchbar = new SearchbarView ({
    id: "searchbar1",
    managerid: "search1",
    el: $("#mysearchbar1")
}).render();

var mysearchcontrols = new SearchControlsView ({
    id: "searchcontrols1",
    managerid: "search1",
    el: $("#mysearchcontrols1")
}).render();

var mytable = new TableView ({
    id: "table1",
    managerid: "search1",
    el: $("#mytable1")
}).render();

// When the timeline changes, update the search manager
mytimeline.on("change", function() {
    mysearch.settings.set(mytimeline.val());
});

// When the query in the searchbar changes, update the search manager
mysearchbar.on("change", function() {
    mysearch.settings.unset("search");
    mysearch.settings.set("search", mysearchbar.val());
});

// When the timerange in the searchbar changes, update the search manager
mysearchbar.timerange.on("change", function() {
    mysearch.settings.set(mysearchbar.timerange.val());
});

 var searchRes = splunkjs.mvc.Components.getInstance("search1");
 var datasource = searchRes.data('results', {count:});
 console.log("Rows: ", datasource.data().rows); 
 console.log("Fields: ", datasource.data().fields); 

});

0 Karma

peamc
Explorer

Thanks, Tom.

That worked perfectly !

0 Karma
Get Updates on the Splunk Community!

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...