I am running into an issue where the JSON produced by a Ruby script is not compatible when parsed by JavaScripts JSON.parse. Consider the following example:
# Ruby
require 'json'
hash = {}
hash["key"] = "value with \u001a unicode"
hash.to_json
=> '{"key":"value with \u001a unicode"}'
// JavaScript
JSON.parse('{"key":"value with \u001a unicode"}')
=> JSON.parse: bad control character in string literal at line 1 column 2 of the JSON data
The issue is the unicode character \u001a. The solution to this is to escape \u001a to \\u001a, but the thing is, the \u001a is automatically inserted into the string by Ruby. I can't reliably post-process the result. Any ideas about how to solve this?
Please note that I wish to call JSON.parse inside a JavaScript execution environment, not inside Ruby's interpreter.
=> "{\"key\":\"value with \\u001a unicode\"}"\\u001ais the terminal is the physical string\u001a. Ruby displays the backslash as \\ so you can tell the difference between the single character\u001aand the six character string also written\u001a.JSON.parseshould be called inside a JavaScript execution environment, not inside the Ruby interpreter.