- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 02:19 AM
Hello fellow scripters,
I'm currently in the process of extracting and associating values from a REST API response body. I have successfully set up the REST message, and the response body is displaying the required values.
Furthermore, I've create a data source and have successfully integrated most of the API call. However, I've reached a point where I'm uncertain about how to extract these values and map them to their respective fields.
Here is the data source script:
(function loadData(import_set_table, data_source, import_log, last_success_import_time) {
var rubrikPost = new sn_ws.RESTMessageV2('Rubrik API Explorer', 'Post serviceAccountId & secret');
var postResponse = rubrikPost.execute();
var postResponseBody = postResponse.getBody();
var postHTTPStatus = postResponse.getStatusCode();
var postParse = JSON.parse(postResponseBody);
var postToken = postParse.token;
gs.log(postResponseBody);
gs.log(postToken);
var rubrikGet = new sn_ws.RESTMessageV2('Rubrik API Explorer', 'Get Nodes');
rubrikGet.setRequestHeader('Authorization', 'Bearer ' + postToken);
var getResponse = rubrikGet.execute();
var getResponseBody = getResponse.getBody();
var getParse = JSON.parse(getResponseBody);
gs.log(getResponseBody);
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
})(import_set_table, data_source, import_log, last_success_import_time);
I've specified the column where I'm attempting to parse the values:
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
This is the response I'm receiving, and I want to extract these and map them. I require the id, brikid, ipAddress and hostname.
I would greatly appreciate any suggestions or recommendations you can provide.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 03:24 AM - edited 09-01-2023 03:35 AM
Hello @Owais3 ,
So as you are already getting a JSON of arrays directly parse the data element in JSON like below instead of parsing the response body.You main data format is JSON and inside that you have arrays so no need to parse the body directly access the data element like below which is an array of JSON's again.
Note :if your response and data element has a single JSON below code will work
var id = "";
var brikid ="";
var ipAddress ="";
var hostname ="";
var data = JSON.parse(getResponseBody.data);
id = data[0].id;
brikid = data[0].brikid;
ipAddress = data[0].ipAddress;
hostname = data[0].hostname;
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
But if you have multiple JSON's in data array its going to a for loop
like below
var id = "";
var brikid ="";
var ipAddress ="";
var hostname ="";
var data = JSON.parse(getResponseBody.data);
for(var i=0;i<data.length; i++)
{
id = data[i].id;
brikid = data[i].brikid;
ipAddress = data[i].ipAddress;
hostname = data[i].hostname;
}
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
Hope this helps
Mark the answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 02:41 AM - edited 09-01-2023 02:42 AM
Hey @Owais3 :
After parsing the data seems to be in a JSON array for fetching the records use a for-in loop and iterate through it to get the data ,see the below snippet and you can modify it
var getParse = JSON.parse(getResponseBody);
var data = getParse.data;
for (var i in data) {
var id = data[i].id; // access id from json array
var brikid = data[i].brikid;
var ipAddress = data[i].ipAddress;
var hostname = data[i].hostname;
}
Mark this as Helpful / Accept the Solution if this clears your issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 03:24 AM - edited 09-01-2023 03:35 AM
Hello @Owais3 ,
So as you are already getting a JSON of arrays directly parse the data element in JSON like below instead of parsing the response body.You main data format is JSON and inside that you have arrays so no need to parse the body directly access the data element like below which is an array of JSON's again.
Note :if your response and data element has a single JSON below code will work
var id = "";
var brikid ="";
var ipAddress ="";
var hostname ="";
var data = JSON.parse(getResponseBody.data);
id = data[0].id;
brikid = data[0].brikid;
ipAddress = data[0].ipAddress;
hostname = data[0].hostname;
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
But if you have multiple JSON's in data array its going to a for loop
like below
var id = "";
var brikid ="";
var ipAddress ="";
var hostname ="";
var data = JSON.parse(getResponseBody.data);
for(var i=0;i<data.length; i++)
{
id = data[i].id;
brikid = data[i].brikid;
ipAddress = data[i].ipAddress;
hostname = data[i].hostname;
}
import_set_table.addColumn('Column1', 400);
var record = {
'column1': 'value',
};
import_set_table.insert(record);
Hope this helps
Mark the answer correct if this helps you
Thanks