Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to extract multiple values from a json response using extract variables policy

My extract variable policy :

<ExtractVariables name="ParsehotelResponse"> 
  <Source>response</Source> 
  <VariablePrefix>response-parse</VariablePrefix> 
  <JSONPayload> 
    <Variable name="Hotel Name" type="Array"> 
      <JSONPath>$.results.name[*]</JSONPath> 
    </Variable> 
    <Variable name="Hotel Rating" type="Array"> 
      <JSONPath>$.results.rating[*]</JSONPath> 
    </Variable> 
    <Variable name="Hotel Address" type="Array"> 
      <JSONPath>$.results.formatted_address[*]</JSONPath> 
    </Variable> 
  </JSONPayload> 
</ExtractVariables>

JavaScript extract policy


response.content = '';
response.headers['Content-Type'] = 'application/json';
// Create a brand-new JSON object for the response and fill it up
var r = response.content.asJSON;
r.Status = context.getVariable('GeoResponse.status');
r.Origin = context.getVariable('GeoResponse.origin');
r.Destination = context.getVariable('GeoResponse.destination');
r.Distance = context.getVariable('GeoResponse.distance');
r.Duration = context.getVariable('GeoResponse.duration');
var hotel = new Object();
hotel.Hotel_Name = context.getVariable('response-parse.Hotel Name{}');
hotel.Hotel_Rating = context.getVariable('response-parse.Hotel Rating{}');
hotel.Hotel_Address = context.getVariable('response-parse.Hotel Address{}');
r.suggestedhotel = hotel

But the hotel details come as null for me. Please help me if i am missing something here .

Sample Response:

{
   "html_attributions" : [],
   "next_page_token" : "CpQCAwEAAF9reYCm5ImOeGu-IF5AQ5BstS1NRn3jAV2v5_ebqQWF9_NaBKOyfFwO8nfT-t51aokWycGed2_1QqOOy2VjaK5dwQy_03wKgsbnZLH7l65OEkhx4gSZd2x8SMVE529jOJ7rt8W7vUxpZbG9SzxcBUSWh0OsTnqbV9etLy7MwElzrEhCrVEW1NJIPvNXQfk68oBA4chIqvniAIioSjCUvKAaAGBDb2kYEoEJdMAu3faMncanxpMVrIQLhBtmM7G7LdmYeqjaQ-ciy-KaJ54UsYbo_dqXNjXukTGuaVz5ayHmasRMgk8-bbvZ5EsZAmRQ5wwfVue-ElTJidDhyfdvcqv00Ga0t2QpHNKjezwSNRmrEhDmE364qxAfLHJepA7ujC6LGhRO1omOQYHjKcj40cTBPEe8n45pFA",
   "results" : [
      {
         "business_status" : "OPERATIONAL",
         "formatted_address" : "35-25 Farrington St, Flushing, NY 11354, United States",
         "geometry" : {
            "location" : {
               "lat" : 40.763661,
               "lng" : -73.83129099999999
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 40.76499557989273,
                  "lng" : -73.83000692010728
               },
               "southwest" : {
                  "lat" : 40.76229592010728,
                  "lng" : -73.83270657989273
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "fdcc641fca120971511b4f7d037b7bfd544f436b",
         "name" : "Magna Restaurant",
         "opening_hours" : {
            "open_now" : false
         },
         "photos" : [
            {
               "height" : 640,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/105271389879554975061\"\u003eMagna Restaurant\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAAnWRyVVzJyCKpoFT0nN-bHNE4voHojmOeQKu7Jk3MSFXe5NMwCE6a-m04JVTK5C_tfIJzPRhg7LN2X9v_HrN-3vbn5XDA82VKYrnekKOc6E9ul4wQTivCKcIANMg9G9sDEhDtQeC7lyzAE0dyoZk7_oZjGhRmOy7uVoXhsWTwbAH7a86eP1jOhA",
               "width" : 480
            }
         ],
         "place_id" : "ChIJT5fPRAVgwokRApCD3ei8UFQ",
         "plus_code" : {
            "compound_code" : "Q579+FF Flushing, Queens, NY, United States",
            "global_code" : "87G8Q579+FF"
         },
         "price_level" : 2,
         "rating" : 4.4,
         "reference" : "ChIJT5fPRAVgwokRApCD3ei8UFQ",
         "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
         "user_ratings_total" : 170
      },
      ...many more...
   ],
   "status" : "OK"
}


Solved Solved
0 14 17.7K
1 ACCEPTED SOLUTION

at last fixed it removed the extract variable and done the same in js as below.

var results_array = r.results;
var new_array = [];
var obj = {};
for (var i=0; i<results_array.length; i++) {
    obj = {
        "name":results_array[i].name,
        "rating":results_array[i].rating,
        "address":results_array[i].formatted_address
    }
    new_array.push(obj)
    
    // Clear the object
    obj = {}
}


r.suggested_restaurant = new_array;








// Delete irrelevant response
delete r.html_attributions;
delete r.next_page_token;
delete r.results

View solution in original post

14 REPLIES 14