George Mauer
George Mauer

Reputation: 122182

ConvertTo-Json throws error when using a string terminating in backslash

The following bit of code generates an error

W:\surge\ogre> @{SolutionDir='W:\Surge\ogre\'} | ConvertTo-Json
ConvertTo-Json : The converted JSON string is in bad format.
At line:1 char:35
+ @{SolutionDir='W:\Surge\ogre\'} | ConvertTo-Json
+                                   ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Collections.Hashtable:PSObject) [ConvertTo-Json], InvalidOperationException
    + FullyQualifiedErrorId : JsonStringInBadFormat,Microsoft.PowerShell.Commands.ConvertToJsonCommand

Why? How could I fix this? This is in a prebuild event so I'm trying to keep it single-line and as compact as possible.

Upvotes: 11

Views: 9192

Answers (1)

Frode F.
Frode F.

Reputation: 54931

Using the -Compress switch should solve your problem. I believe this is a bug in PowerShell. Others have had similar problem with ex. values containing double quotes

Upvotes: 33

Related Questions