Including json string in url encoded string (Rails)
How do I convert this json var
email = {"email":"name@gmail.com"}
into this encoded string?
%7B%22email%22%3A%22name%40gmail.com%22%7D
+4
Jack zerby
source
to share
2 answers
You can of course use the library uri
shown here
[2] pry(main)> require 'uri'
=> true
[3] pry(main)> URI.encode('{"email":"name@gmail.com"}')
=> "%7B%22email%22:%22name@gmail.com%22%7D"
+3
Anthony
source
to share
Use CGI.escape , not URI.encode / escape. URI.encode will not go outside the brackets of JSON arrays.
emails = '{"list_1":[{"Jim":"jim@gmail.com"},{"Joe":"joe@gmail.com"}]}'
> URI::encode(emails)
=> "%7B%22list_1%22:[%7B%22Jim%22:%22jim@gmail.com%22%7D,%7B%22Joe%22:%22joe@gmail.com%22%7D]%7D"
> CGI.escape(emails)
=> "%7B%22list_1%22%3A%5B%7B%22Jim%22%3A%22jim%40gmail.com%22%7D%2C%7B%22Joe%22%3A%22joe%40gmail.com%22%7D%5D%7D"
ruby - what's the difference between URI.escape and CGI.escape - stack overflow
0
Yarin
source
to share