Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting URL to JSON version?

On all the pages of my app, I want a link to the JSON version of current page. Any neat tricks to do this? Where it got complicated was when additional '&' parameters were included in the URL.

So the urls would be transposed as:

'/users' => '/users.json'

'/users?page=1&per_page=5' => '/users.json?page=1&per_page=5'
like image 387
Carson Cole Avatar asked Dec 13 '11 18:12

Carson Cole


People also ask

How do I save a URL as a JSON file?

Using the Chrome browser, go to the url with the json, then right click, then 'Inspect'. That brings up the Chrome devtools ui. From there go to 'Sources' and you will see the json file in the list. Then right click and you will be able to click 'Save as'.

How do I get JSON data from a website?

getJSON method loads JSON-encoded data from a server using a GET HTTP request. This is the method signature. The url parameter is a string containing the URL to which the request is sent. The data is a plain object or string that is sent to the server with the request.

Can we convert HTML to JSON?

From HTML to JSON allows loading the Website URL which has tables converting to JSON. Click on the URL button, Enter URL and Submit. Parsing HTML into JSON supports loading the HTML File to transform to JSON. Click on the Upload button and select File.

What is URL encoded JSON?

Json url-encoder tool What is a json url-encoder? This tool converts JavaScript Object Notation (JSON) data to URL-encoding. All special URL characters get escaped to percent-number-number format. Json url-encoder examples Click to use. URL-escape a JSON Array.


2 Answers

try this :

polymorphic_path( @user, :format => :json )

(as seen in this API doc)

alternatively :

user_path( :id => @user.id, :format => :json )
like image 195
m_x Avatar answered Sep 28 '22 00:09

m_x


Attribution for this answer from Get url for current page, but with a different format, with modifications:

Helper:

def current_url(new_params)
 url_for params.merge(new_params)
end

Link:

<%= link_to "JSON of this page", current_url(:format=>:json)
like image 37
Carson Cole Avatar answered Sep 28 '22 01:09

Carson Cole