ruby - How to extract each JSON object in an array and feed into API request body -


i have array of 647 json objects , want extract each object , feed them 1 one request body of api. api accept each json object separately individual http request each object. how do this?

this have far using basic each block in ruby httparty gem:

require "json" require "httparty"  restaurants = json.parse file.read('pretty-regex2.json')  new_rest_variable = restaurants.each |restaurant| end  response = httparty.post("https://api.example/placeholder", {   :body => new_rest_variable.to_json,   :headers => { "content-type" => "text", "accept" => "application/x-www-form-urlencoded", "authorization" => "token example-placeholder" }   })  puts response.body puts response.code puts response.message 

example of 4 "restaurant objects" enclosed in array square brackets 647:

[   {     "id": "223078",     "name": "3 south place",     "phone": "+442032151270",     "email": "3sp@southplacehotel.com",     "website": "",     "location": {       "latitude": 51.5190536,       "longitude": -0.0871038,       "address": {         "line1": "3 south place",         "line2": "",         "line3": "",         "postcode": "ec2m 2af",         "city": "london",         "country": "uk"       }     }   },   {     "id": "210071",     "name": "5th view bar & food",     "phone": "+442077347869",     "email": "waterstones.piccadilly@elior.com",     "website": "http://www.5thview.com",     "location": {       "latitude": 51.5089594,       "longitude": -0.1359897,       "address": {         "line1": "waterstone's piccadilly",         "line2": "203-205 piccadilly",         "line3": "",         "postcode": "w1j 9ha",         "city": "london",         "country": "uk"       }     }   },   {     "id": "239971",     "name": "65 & king",     "phone": "+442072292233",     "email": "hello@65king.com",     "website": "http://www.65king.com/",     "location": {       "latitude": 51.5152533,       "longitude": -0.1916538,       "address": {         "line1": "65 westbourne grove",         "line2": "",         "line3": "",         "postcode": "w2 4uj",         "city": "london",         "country": "uk"       }     }   },   {     "id": "131543",     "name": "abbey",     "phone": "+442079682400",     "email": "info@abbey-bar.co.uk",     "website": "http://www.abbey-bar.co.uk",     "location": {       "latitude": 51.51241,       "longitude": -0.0751462,       "address": {         "line1": "st clare house",         "line2": "30-33 minories",         "line3": "",         "postcode": "ec3n 1dd",         "city": "london",         "country": "uk"       }     }   } ] 

try:

require "json" require "httparty"  restaurants = json.parse file.read('pretty-regex2.json')  restaurants.each |restaurant|   response = httparty.post("https://api.example/placeholder",   {     :body => restaurant.to_json,     :headers => { "content-type" => "text", "accept" => "application/x-www-form-urlencoded", "authorization" => "token example-placeholder" }     })    puts response.body   puts response.code   puts response.message end 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -