I'm trying to embed a ruby hash into a json string that can be parsed by javascript JSON.parse.
I'm having escaping issues.
Ruby:
props = {sring: 'He said "hey"'}
HAML
:javascript
const props = JSON.parse('#{props.to_json}');
Which renders as
<script>
const props = JSON.parse('{"sring":"He said \"hey\""}');
</script>
Which throws the following error:
Uncaught SyntaxError: Expected ',' or '}' after property value in JSON at position 19
at JSON.parse (<anonymous>)
at (index):58:24
I've experiemented a bit and the following string is parseable by JSON.parse, but not sure how to produce it from an escape function:
<script>
const props = JSON.parse('{"sring":"He said \\\"hey\\\""}');
</script>