Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass a JSON string without using backslashes

Using backslashes to pass a JSON string as a parameter in PowerShell is cumbersome.

executable --json-input '{ \"name\": \"Bob\" }' output.txt

Is there a way to avoid using these backslashes? I tried using single quotes, and doubles quotes in and out without any success. In Python I use triple quotes print(""" here is an example "" """) to avoid character escaping.

Is there a similar solution in PowerShell? One where we never need to worry about reformating a JSON string?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

PowerShell has here-strings, similar to the multiline literals from Perl (<<<) or Python (""").

The starting quote must be preceded by @ and immediately followed by a line break, whereas the closing quote must follow a newline and be followed by another @ sign:

command --json-input @"
{ "name": "Bob" }
"@ output.txt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading