Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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.

like image 377
George Mauer Avatar asked May 08 '14 20:05

George Mauer


1 Answers

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

like image 152
Frode F. Avatar answered Oct 17 '22 17:10

Frode F.