1

I am trying to implement SMS API in rails, when I hit the API url I get following html response:

<!DOCTYPE RESULT SYSTEM "http: api_link ">
<html><body>
<result reqid="57469">
    <mid submitdate="2015-12-02 00:51:55" id="1" tag="null" tid="103335">

    </mid>
</result>
</body></html>

How can I convert this to json?

Imran Ahmad
  • 2,798
  • 3
  • 28
  • 49
  • How do you print that data to the view? – bork Dec 01 '15 at 19:46
  • I am rendering it in controller only. – Imran Ahmad Dec 01 '15 at 19:47
  • 1
    Yes, obviously. Could you post that code as well? – bork Dec 01 '15 at 19:48
  • No, I want to convert it to json so that it could be read by android developer. Depending upon he will show some message to user. – Imran Ahmad Dec 01 '15 at 19:51
  • 1
    I just wanted to see if you're passing the data through a hash, because if you are, there's a neat thing called to_json that you could use, http://apidock.com/rails/Hash/to_json – bork Dec 01 '15 at 19:53
  • Initially I did .to_json but then I realized it works with array not with html, – Imran Ahmad Dec 01 '15 at 19:58
  • I don't believe you can parse html objects to JSON (someone more front end, feel free to correct me), but you have to do it in your Ruby code, preferably in your model and not in the controller. – bork Dec 01 '15 at 20:00
  • Hi @Emmy and welcome to Stack Overflow. It's really hard for us to debug a verbal description of your code. We need to see the *actual* code in order to help you. Can you please edit your question and add in all the relevant code (the controller code etc). If you don't do this, then we cannot help you. Note: please don't put code in comments (like here), because the formatting is dreadful - edit your question and add the code there. – Taryn East Dec 01 '15 at 23:39

2 Answers2

3

Unfortunately at this time you cannot convert html straight to json, but you can convert html to a hash and then the hash to json.

YOUR_HTML_CODE = '<!DOCTYPE RESULT SYSTEM "http: api_link "><html><body> <result reqid="57469"> <mid submitdate="2015-12-02 00:51:55" id="1" tag="null" tid="103335"> </mid></result></body></html>'

@data = Hash.from_xml(YOUR_HTML_CODE).to_json

Returns:

=> "{\"html\":{\"body\":{\"result\":{\"reqid\":\"57469\",\"mid\":{\"submitdate\":\"2015-12-02 00:51:55\",\"id\":\"1\",\"tag\":\"null\",\"tid\":\"103335\",\"__content__\":\"\\n\\n    \"}}}}}"

To find more ways to do what you want, search for rails xml to json, you will find more answers on this question. Crack gem seems to be excellent for this.

Note that if you are using rails you should be handling this on the backend, but that was not as helpful of an answer by itself, thus my answer above.

Community
  • 1
  • 1
Bryan Dimas
  • 1,389
  • 13
  • 18
0

If your api only returns json you can set format resource.

Example:

config/routes.rb

Rails.application.routes.draw do  
  resources :posts, defaults: { format: :json }
end

If your api returns html, xml,and json you can set in your controller(remove defaults: { format: :json } in config/routes.rb):

app/controller/posts_controller.rb

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @post }
  format.json { render :json => @post }
end
kalelc
  • 1,507
  • 1
  • 18
  • 33