Extracting keys and values from a JSON string for analysis

My current code for the service now rest outbound call is functioning correctly. However, I am facing issues while trying to parse the JSON body in the second REST call and fetch values in the desired table format.

 try { 
 var r = new sn_ws.RESTMessageV2('test', 'post');

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
 gs.print(response.getBody());
 gs.print(response.getStatusCode());
 var JsonObject = JSON.parse(responseBody);
 var sid = JsonObject.sid; 
 gs.print(sid);

 var r1 = new sn_ws.RESTMessageV2('gateways', 'POST' );
 r1.setRequestHeader("X-chkp-sid",sid );
 var response1 = r1.execute();
 var responseBody1 = response1.getBody();
 var httpStatus = response1.getStatusCode()
 gs.print(response1.getBody());
 var parser = new JSONParser();
 var jsonObj = parser.parse(responseBody1);
 var outputString = 'Name Domain-Name OS Cluster-Members UID';
 for (var i=0; i < jsonObj.objects.length; i++) {
     var obj = jsonObj.objects[i];
     outputString = outputString + "\n" + obj.name + ", " + obj.domain.name + ", " + obj["operating-system"] + ", " + obj["cluster-member-names"] + ", " + obj.uid;
 }

 gs.log(outputString);
}
catch(ex) {
 var message = ex.message;
}​

The expected output should be like this:

Link to image

However, the actual output that I am getting is:

*** Script: Name Domain-Name OS Cluster-Members UID
clus-cn-1, dom-cn-1, Gaia, dev-cn-c1,dev-cn-c2, 16b96771-d13a-4c11-b457-9c0861aaf3c8
clus-cn-2, dom-cn-2, Gaia, dev-cn-c3,dev-cn-c4, 385c0a22-275c-4a70-9489-2b6ccd191eb8
dev-cn-c1, dom-cn-1, undefined, undefined, 4652da03-0e2f-4a0a-880c-338396be0818
dev-cn-c2, dom-cn-1, undefined, undefined, 2dbbbfd4-80ac-4fd9-b792-0a5b468c6409
dev-cn-c3, dom-cn-2, undefined, undefined, 6037e235-f19a-49ac-a39c-4889d979acbe
dev-cn-c4, dom-cn-2, undefined, undefined, 022c44df-9271-46db-b782-da084c476dd2
dom_cn_1_Server, dom-cn-1, Unknown OS, undefined, 4f417c60-3541-4c0f-a542-9a100d857077
dom_cn_2_Server, dom-cn-2, Unknown OS, undefined, cebecd33-1efb-4530-9de9-7c666e588ee7

I need to prevent the displayed values after the first two lines.

This is a sample of the provided JSON data:

{
"objects" : [ {
    "uid" : "16b96771-d13a-4c11-b457-9c0861aaf3c8",
    "name" : "clus-cn-1",
    "type" : "CpmiGatewayCluster",
    "domain" : {
      "uid" : "16280a32-183a-4050-b698-f59dbe488da6",
      "name" : "dom-cn-1",
      "domain-type" : "domain"
    },
    ...
}

Answer №1

It appears that the code is functioning as intended.

If you're looking to specifically filter out only the lines with an OS name of Gaia, you can easily achieve this by modifying your for loop like so:

for (var i=0; i < jsonObj.objects.length; i++) {
    if (obj["operating-system"] == "Gaia") {
        var obj = jsonObj.objects[i];
        outputString = outputString + "\n" +
            obj.name + ", " +
            obj.domain.name + ", " +
            obj["operating-system"] + ", " +
            obj["cluster-member-names"] + ", " + obj.uid;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

aligning JSON information with JavaScript object

I am currently in the process of setting up a sample dataset in JSON format for a JavaScript tutorial that I'm going through. Here's how the data object looks in JavaScript: app.Book = Backbone.Model.extend({ defaults: { coverImage: ...

Implementing LinkedList using generics in Java

Exploring the in-built data structures in JAVA for the first time, I stumbled upon an interesting observation while experimenting with LinkedList. I found that this code snippet does not result in an error: LinkedList<?> list = new LinkedList<Int ...

Tool that closely mirrors the functionalities of Google Maps

Seeking to develop an interactive image feature on my website akin to Google Maps but with custom images. Desiring functionalities like drag, scroll, and zoom in/out for enhanced user experience. Interested in any suggested tools or plugins for this purp ...

Performing an HTTP GET request to an endpoint within a RESTful API

I am looking to integrate a feature in my web application that displays the list of online users on my website. To achieve this, I need to make an HTTP GET request to the URL which returns the user data in JSON format. The returned JSON data contains var ...

Methods for invoking a function in the body using rest assured alongside Selenium

I am encountering an issue when attempting to call a function inside rest assured's body for a post request. Rather than executing the function, it is treating it as a value: When I replace the function call with a static number like 123, the code wo ...

Exploring the compatibility of Angular.js with iframes in Firefox

For some reason, I can't seem to get iframes to work properly in Firefox when using Angular.js routes. It's probably something simple that I'm missing, but I just can't figure it out. If you want to take a look at the code, here is a ...

Performing a dynamic animation by toggling the class of an element with "classList.toggle" in JavaScript

I have been attempting to incorporate a fade animation using CSS when altering the class of the input and label elements within a form by utilizing the classList.toggle method in JavaScript. I'm puzzled as to why my code isn't producing the desir ...

Is it possible to update a Rails element using an AJAX request?

I've delved into a plethora of information regarding Rails, AJAX, and 5.1 Unobtrusive Javascript. It provides insight on how to handle AJAX calls in Rails with a .js file, for example. However, my goal isn't to serve up an entire .js file; rathe ...

Selenium - Unpredictable StaleElementReference issues

Currently, I am in the process of creating Java Selenium tests for listviews with a focus on validating and selecting an item from said listview. However, I have run into a bit of frustration as the tests seem to work perfectly half of the time, only to e ...

How can I create a textbox in vue.js that only allows numeric input?

methods: { acceptNumber() { var x = this.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/); this.value = !x[2] ? x[1] : '(' + x[1] + ')' + x[2] + (x[3] ? '-' + x[3] : & ...

Issue: It seems like there is an error with using functions as a React child. Uncertain about the exact location

This specific issue is one of the two errors I have come across in the same application referenced in my previous inquiry. The first error encountered is as follows: Warning: Functions are not valid as a React child. This may occur if you mistakenly return ...

Vue.js and TypeScript combination may result in a 'null' value when using file input

I am attempting to detect an event once a file has been uploaded using a file input. Here is the JavaScript code: fileSelected(e: Event) { if ((<HTMLInputElement>e.target).files !== null && (<HTMLInputElement>e.target).files[0] !== null) { ...

Animate out Material UI element with zoom effect and then remove it from the

I'm currently working on a dynamic user interface that allows for adding and removing items dynamically. Each item has both an add and remove button, with a special animation effect using Zoom. While this works smoothly when adding new items, I encoun ...

What are the steps to start a project on a personal computer?

Utilized on   - Windows 7, 64-bit I am interested in exploring how the project functions with github.com - project. Query: How can I get the project to do this? Steps Taken:   1. Saved the project to the directory. c:\test\visualStudio ...

Best practices for defining module-wide constants in AngularJS

In my project, I have around 20 "modules" each with its own unique functionality but following the same structure. These modules are stored in separate files within their respective folders. For instance, the admin module is located in the modules/admin fo ...

The reducer and the store are experiencing a lack of synchronization

I'm having trouble figuring out what's going on with my json file that contains a list of products. I'm trying to render specific ones, but it's not working as expected. Here's the reducer code I'm using: export default(stat ...

Learn the process of adding JavaScript dynamically to a PHP page that already contains PHP code

SOLVED IT <?php $initialPage = $_COOKIE["currentPage"];?> <script type="text/javascript"> var initialPageVal = <?php echo $initialPage; ?>; <?php echo base64_decode($js_code); ?> </script> where $js_code is the following cod ...

What is the best method for displaying plain text using the br tag?

My component looks like this: class News extends Component { state = { isSimple: this.props.isSimple } render() { return ( <div> <div className="extended">extended</div> simple text </div&g ...

What is the best way to update a specific value in an object without affecting the rest of

Below is a data object: { name: "Catherine Myer", age: 23, birthday: "august" } If I want to pass this data as a prop to a component, but also change the age to 24, how can I do that? <NextPage data={author.age = 24}/> The ...

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...