Getting Data In

JavaScript SDK Normal Search

rickshaw12
Explorer

Hello all,
I am trying to create a JavaScript SDK search. I am getting the data I want thru the row and field like so:

Async.chain([

            function(done) {
                service.login(done);
            },
            // Perform the search
            function(success, done) {
                if (!success) {
                    done("Error logging in");
                }

                service.search("search XXXXXX | spath YYYYYYY | head 10", {}, done);
            },
            // Wait until the job is done
            function(job, done) {
                Async.whilst(
                    // Loop until it is done
                    function() { return !job.properties().isDone; },
                    // Refresh the job on every iteration, but sleep for 1 second
                    function(iterationDone) {
                        Async.sleep(1000, function() {
                            // Refresh the job and note how many events we've looked at so far
                            job.fetch(function(err) {
                                iterationDone();
                            });
                        });
                    },
                    // When we're done, just pass the job forward
                    function(err) {
                        done(err, job);
                    }
                );
            },
            // Print out the statistics and get the results
            function(job, done) {      
                // Ask the server for the results
                job.results({}, done);
            },
            // Print the raw results out
            function(results, job, done) {
                // Find the index of the fields we want
                var rawIndex = utils.indexOf(results.fields, "YYYYYYY");


                // Print out each result and the key-value pairs we want
                document.write('{"RESULTS":"10", "ZIPS":[');


                for(var i = 0; i < results.rows.length; i++) {
                    if(i>0) document.write(",");
                    //console.log(results.rows[i][rawIndex]);
                    document.write('"'+ results.rows[i][rawIndex] +  '"' );
                    if (i== 9) document.write(']}');
                }

                // Once we're done, cancel the job.
                job.cancel(done);
            }
        ],
        function(err) {;        
        }
    );

What I want to do is to grab the YYYYY that I am document.write to the page and use it as a JSON file for another page. The problem I am having is I want to import the YYYYY data as a JSON file on my other webpage but I do not know how to write that info into a JSON file. Any help?

0 Karma
1 Solution

smohamed
Splunk Employee
Splunk Employee

Hi Rick,

From what I understand, you need help on writing data to a file.
What you can do is put all the data you want in a variable, then write it to a file as shown by the top answer on this Stack Overflow question.

Instead of passing the "Hey there!" string as the second argument, pass your the data you want written to the file you specify as the first argument.

-Shakeel

View solution in original post

smohamed
Splunk Employee
Splunk Employee

Hi Rick,

From what I understand, you need help on writing data to a file.
What you can do is put all the data you want in a variable, then write it to a file as shown by the top answer on this Stack Overflow question.

Instead of passing the "Hey there!" string as the second argument, pass your the data you want written to the file you specify as the first argument.

-Shakeel

smohamed_splunk
Splunk Employee
Splunk Employee

Alright, the Splunk JavaScript SDK is built on top of Node.js & requires it. Node.js is a server-side JavaScript framework, similar in nature to PHP or Ruby on Rails.

Make sure you have the JS SDK requirements here: http://dev.splunk.com/view/SP-CAAAED6

Currently, the SDK only supports up to v0.8.x of Node.js but we're releasing an update mid-August which will support up to v0.10.x.
You can download a specific version of Node.js here: http://nodejs.org/dist/

Once installed, you can run JavaScript files from the command line by entering in "node [yourfilename].js" without the quotes.

0 Karma

rickshaw12
Explorer

Thank you very much for the response, I am having difficulties writing to the file though. It is coming up as required not defined when I try to require the variable.I now know that I have to do this not in the browser but I am confused now where to do this because so far I have done everything client side

0 Karma
Get Updates on the Splunk Community!

Cloud Platform & Enterprise: Classic Dashboard Export Feature Deprecation

As of Splunk Cloud Platform 9.3.2408 and Splunk Enterprise 9.4, classic dashboard export features are now ...

Explore the Latest Educational Offerings from Splunk (November Releases)

At Splunk Education, we are committed to providing a robust learning experience for all users, regardless of ...

New This Month in Splunk Observability Cloud - Metrics Usage Analytics, Enhanced K8s ...

The latest enhancements across the Splunk Observability portfolio deliver greater flexibility, better data and ...