07-18-2023, 06:45 PM
I am passing some JSON to a server via a script (not mine) that accepts the JSON as a string.
Some of the content of the JSON contains single quotes so I want to ensure that any single quotes are escaped before being passed to the script.
I have tried the following:
> irb
> 1.9.3p194 :001 > x = "that's an awesome string"
> => "that's an awesome string"
> 1.9.3p194 :002 > x.sub("'", "\'")
> => "that's an awesome string"
> 1.9.3p194 :003 > x.sub("'", "\\'")
> => "thats an awesome strings an awesome string"
but can't seem to get the syntax right.
Some of the content of the JSON contains single quotes so I want to ensure that any single quotes are escaped before being passed to the script.
I have tried the following:
> irb
> 1.9.3p194 :001 > x = "that's an awesome string"
> => "that's an awesome string"
> 1.9.3p194 :002 > x.sub("'", "\'")
> => "that's an awesome string"
> 1.9.3p194 :003 > x.sub("'", "\\'")
> => "thats an awesome strings an awesome string"
but can't seem to get the syntax right.