VMware Cloud Community
LionelGourdet
Contributor
Contributor
Jump to solution

How to parse REST requests results ?

Hello,

I created a workflow to automatise the creation of a vm and his configuration under RedHat Satellite. In order to make the RedHat Satellite configuration, I need to run a REST request (with GET method) and then I have to parse the results but I don't find the way to do this.

The request.execute() method that I use have no function available to parse the results. The only method that I find "usefull" is contentAsString but it return a string.

Could you help me to solve this ?

Here is an example of the contentAsString results, I need to get the id result :

  1. {
  2.     "total": 61,
  3.     "subtotal": 1,
  4.     "page": 1,
  5.     "per_page": 20,
  6.     "search": "GP-PROD-CentOs-7.2",
  7.     "sort":
  8.     {
  9.         "by": null,
  10.         "order": null
  11.     },
  12.     "results":
  13.     [
  14.         {
  15.             "subnet_id": null,
  16.             "subnet_name": null,
  17.             "operatingsystem_id": 23,
  18.             "operatingsystem_name": "CentOS 7.2",
  19.             "domain_id": 1,
  20.             "domain_name": "si.francetv.fr",
  21.             "environment_id": 35,
  22.             "environment_name": "KT_Default_Organization_Library_VUE_CentOS_7_2_25",
  23.             "compute_profile_id": 1,
  24.             "compute_profile_name": "1-Profile_de_VM-Petite",
  25.             "ancestry": null,
  26.             "parent_id": null,
  27.             "parent_name": null,
  28.             "puppet_proxy_id": 1,
  29.             "puppet_ca_proxy_id": 1,
  30.             "ptable_id": 108,
  31.             "ptable_name": "FTV Disk Layout CentOS",
  32.             "medium_id": 19,
  33.             "medium_name": "CentOs 7.2",
  34.             "architecture_id": 1,
  35.             "architecture_name": "x86_64",
  36.             "realm_id": null,
  37.             "realm_name": null,
  38.             "created_at": "2016-05-02 15:18:44 UTC",
  39.             "updated_at": "2016-10-19 13:51:43 UTC",
  40.             "id": 56,
  41.             "name": "GP-PROD-CentOs-7.2",
  42.             "title": "GP-PROD-CentOs-7.2"
  43.         }
  44.     ]
  45. }
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

In this case, you can parse the string returned from contentAsString property as a JSON object, and then access the various object properties as you would do with a normal Javascript object.

For your example, the following code should return the value of the id property (number 56):

var contentAsString = ...; // contentAsString property of the response after request.execute()

var json = JSON.parse(contentAsString);

System.log("id -> " + json.results[0].id);

View solution in original post

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

In this case, you can parse the string returned from contentAsString property as a JSON object, and then access the various object properties as you would do with a normal Javascript object.

For your example, the following code should return the value of the id property (number 56):

var contentAsString = ...; // contentAsString property of the response after request.execute()

var json = JSON.parse(contentAsString);

System.log("id -> " + json.results[0].id);

0 Kudos
LionelGourdet
Contributor
Contributor
Jump to solution

Hello,

Thanks for your fast and efficent answer !

I can now run my REST request a parse all the informations that I need !

Regards,

Lionel

0 Kudos