ML99999999
ML99999999

Reputation: 1

How to escape the single quotation json running in PowerShell

I need to create the Body as below but the single quotation mark around ABC gets syntax error when running in PowerShell.

    $Body = '{
    “Tag”:"Reference='ABC'"
    }'

    Write-Host -Message "$Body"

I tried the below solution:

    $MyVar = 'BRO001023'

    $Body = '{
    “Tag”:"Reference=$MyVar"
    }'

    Write-Host -Message "$Body"

BUT I got the result as below.

    {
    “Filter”:"Reference=$MyVar"
    }

How can I make it shows like { “TagFilter”:"Reference='ABC'" }

Upvotes: 0

Views: 236

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

You can either use a verbatim/single-quoted here-string:

$Body = @'
{
    "Tag":"Reference='ABC'"
}
'@

Or escape all inline ''s by doubling them:

$Body = '{
    "Tag":"Reference=''ABC''"
}'

See the about_Quoting_Rules help topic for more information about quoting rules for different string literals

Upvotes: 1

Related Questions