0

I am implementation of data from a JSON file to a Ruby on Rails application by using HTTparty gem, but I am getting Error no implicit conversion of String into Integer

I refered this tutorial link

My json data,

{
    "restaurant_name": "Restaurant 3",
    "address": "xyz address",
    "country": "United States",
    "currency": "USD",
    "client_key": "12345",
    "client_name": "Client 3",
    "client_email": "[email protected]",
    "client_phone": "9876",
    "product_tier": "tier1",
    "brand_logo_large": {
        "ID": 37,
        "id": 37,
        "title": "bait-al-bahar-logo-design",
        "filename": "bait-al-bahar-logo-design.png",
        "filesize": 105071,
        "url": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
        "link": "http://codekyt.in/froodle-wp/projects/res-1-client-1/bait-al-bahar-logo-design/",
        "alt": "",
        "author": "1",
        "description": "",
        "caption": "",
        "name": "bait-al-bahar-logo-design",
        "status": "inherit",
        "uploaded_to": 35,
        "date": "2019-01-04 11:11:48",
        "modified": "2019-01-04 11:13:01",
        "menu_order": 0,
        "mime_type": "image/png",
        "type": "image",
        "subtype": "png",
        "icon": "http://codekyt.in/froodle-wp/wp-includes/images/media/default.png",
        "width": 600,
        "height": 500,
        "sizes": {
            "thumbnail": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-150x150.png",
            "thumbnail-width": 150,
            "thumbnail-height": 150,
            "medium": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-300x250.png",
            "medium-width": 300,
            "medium-height": 250,
            "medium_large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
            "medium_large-width": 600,
            "medium_large-height": 500,
            "large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
            "large-width": 600,
            "large-height": 500
        }
    }

In model,

include HTTParty

In controller

def index  
  require 'httparty'
  @category = HTTParty.get(
    'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
    :headers =>{'Content-Type' => 'application/json'}
  )
end

In gemfile,

gem 'httparty'
gem 'json'

In view,

<%@category.each do |category|%>
<%=category["restaurant_name"]%>
<%=category["country"]%>
<%=category["currency"]%>
<%=category["usd"]%>
<%=category["client_key"]%>
<%=category["client_name"]%>
<%=category["client_email"]%>
<%=category["client_phone"]%>
<%=category["product_tier"]%>
<%=category["brand_logo_large"]%>
<%end%>

1 Answer 1

2

HTTParty.get returns an encapsulated response type of class HTTParty::Response, you need to retrieve the parsed response from that by:

response = HTTParty.get(
  'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
  :headers =>{'Content-Type' => 'application/json'}
)
@category = response.parsed_response # this will return the json.

Also you do not need to iterate on @category in your case, the json object is singular and you can directly use it:

<%=@category["restaurant_name"]%>
<%=@category["country"]%>
<%=@category["currency"]%>
<%=@category["usd"]%>
<%=@category["client_key"]%>
<%=@category["client_name"]%>
<%=@category["client_email"]%>
<%=@category["client_phone"]%>
<%=@category["product_tier"]%>
<%=@category["brand_logo_large"]%>
Sign up to request clarification or add additional context in comments.

8 Comments

@kiddorails no my json object is not sigular for example purpose i show only one data
In which case u can iterate. But the sample json you have provided and on the mentioned URL is singular.
@kiddorails now getting same error no implicit conversion of String into Integer.
@rajD on which line? in parsed_response? what does response.code give you?
i use this way but same error :<%= puts @category.collect {|p| "#{p[:restaurant_name]}: #{p[:country]}"}%>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.