Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Escaping JSON

I'm working on an external API.

They want JSON as a string for a GET request in a query param.

The problem with Ruby is that it treats JSON as a string and escapes quotes, so we end up sending:

"{\"api_key\":\"9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8\",\"ip\":\"184.61.23.239\"}

Instead of {"api_key":"9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8","ip":"184.61.23.239"} as their system is expecting.

Any idea the best way to handle formatting our JSON to match their criteria?

like image 692
Tom Hammond Avatar asked Apr 22 '26 17:04

Tom Hammond


1 Answers

You could use JSON.parse, i.e.,

JSON.parse("{\"api_key\":\"9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8\",\"ip\":\"184.61.23.239\"}") 

returns

{"api_key" => "9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8","ip" => "184.61.23.239"}
like image 100
fl9 Avatar answered Apr 25 '26 08:04

fl9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!