I'd like to call a command line utility from a ruby script like so:
#!/env/ruby
json = {"key" => "value that \"has\" quotes"}.to_json
`aws events put-targets --cli-input-json #{json}`
Such that the resultant call should look like:
aws events put-targets --cli-input-json "{\"key\": \"value that \"has\" quotes\"}"
However, the result in this string interpolation results in a clean looking json structure without the quotes escaped and so results in error at the command line. Eg.
aws events put-targets --cli-input-json {"key": "value that \"has\" quotes"}
I need all the quotes properly escaped so that a string to the command line can be parsed as proper json.
I've tried doing string manipulation to manually escape quotes with things like:
json.gsub(/\"/,'\"')
But that doesn't work either.
This seems like it's harder than it should be. How can I render a properly escaped json string to the command line call?
I do have a rails environment that I can run this through if there are utilities that ActiveSupport provides that would facilitate this.