I have the following JSON
file (it's a bundleconfig.json
file from a VS project):
[
{
"outputFileName": "Scripts/a.js",
"inputFiles": [ "Scripts/b.js", "Scripts/c.js" ]
},
...
]
and I'm parsing it like so:
$json = Get-Content $filePath -Encoding UTF8 | `
Out-String | `
ConvertFrom-Json
I expected the result $json
to be an Array
but it seems that it's not. When I try to see the contents of the array:
$json | ForEach-Object { $_ }
nothing is printed out (correction: It prints empty lines). So, I though that $json
isn't actually an array and tried to find out its type:
$json.GetType()
$json | ForEach-Object { $_ }
And here is the strange part. $json.GetType()
prints out System.Array
(among other things) and now the ForEach-Object
works as initially expected!
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
8
outputFileName : Scripts/a.js
inputFiles : {Scripts/b.js,Scripts/c.js}
So, my code only works if the $json.GetType()
command is present, otherwise it simply doesn't.
I could simply leave the $json.GetType()
command and get done with it, but I have to know the reason behind this behavior.
Any thoughts?
UPDATE:
- It's
PSVersion: 5.0.10586.122
- Both files are
UTF-8
- This code is part of a psake build script, maybe this has something to with it?
- The file doesn't seem to be the culprit, I tested with a dummy file I made on the fly and the behavior is the same.
- I can't even reproduce it myself outside the build script. I made a repro folder with the
.json
and those few lines of code (without the$json.GetType()
command), and it works as expected.