If you just want a manual one-off solution to escape some text on the fly and are using a Windows machine, this PowerShell solution will work from a clean install of the OS with no other tools:
[PSCustomObject] @{
'foo' = 'Hello, World!'
'bar' = 'Goodbye, World!'
} | ConvertTo-JSON
Result:
{
"foo": "Hello, World!",
"bar": "Goodbye, World!"
}
If you're not familiar with PowerShell, these single quotes work like UNIX hard quotes; the only escape you'll need is for more sinqle quotes. Double quotes are often more convenient if your data has single quotes, but certain characters will need to be escaped with back ticks:
[PSCustomObject] @{
'foo' = 'two single quotes: '''' and two dollar signs: $$'
'bar' = "two single quotes: '' and two dollar signs: `$`$"
} | ConvertTo-JSON
Result:
{
"foo": "two single quotes: \u0027\u0027 and two dollar signs: $$",
"bar": "two single quotes: \u0027\u0027 and two dollar signs: $$"
}
Piping to clip.exe will output the result to your clipboard so you can paste it somewhere else:
[PSCustomObject] @{
'foo' = 'some quotes: """'
'bar' = 'picket fence: /\/\/\'
} | ConvertTo-JSON | clip
No result is displayed, but this is now in the user's clipboard:
{
"foo": "some quotes: \"\"\"",
"bar": "picket fence: /\\/\\/\\"
}
'
, you're doomed from the start: JSON strings can only be surrounded with"
. See ietf.org/rfc/rfc4627.txt. – FernStringEscapeUtilities
outline. Its pretty useful. – Trombley