Reputation: 6090
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?
Upvotes: 3
Views: 168
Reputation: 1622
Another solution is using gem 'oj'. I prefer to use this gem, because it so fast and good performance.
Using with oj like this:
j = Oj.load("{\"api_key\":\"9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8\",\"ip\":\"184.61.23.239\"}")
It will be:
=> {"api_key"=>"9e4b33422adb-3832c7-41379-b2f31-8fc295aefb8c8", "ip"=>"184.61.23.239"}
Upvotes: 0
Reputation: 434
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"}
Upvotes: 4