0

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:

  1. It's PSVersion: 5.0.10586.122
  2. Both files are UTF-8
  3. This code is part of a psake build script, maybe this has something to with it?
  4. 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.
  5. 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.
gravity
  • 2,175
  • 2
  • 26
  • 34
lalibi
  • 3,057
  • 3
  • 33
  • 41
  • 3
    I cannot reproduce. – Roman Kuzmin Jul 07 '16 at 19:40
  • I'm unable to reproduce as well... what version of `PowerShell` does this specficially relate to (`$PSVersionTable` should provide this as `PSVersion`) – gravity Jul 07 '16 at 20:25
  • Does it print nothing or empty lines? Are you outputting anything else from you script? – user4003407 Jul 07 '16 at 20:44
  • Could you post the actual full json (values obfuscated if needed)? – Mathias R. Jessen Jul 07 '16 at 22:36
  • Please see my update at the bottom. Thank you. – lalibi Jul 07 '16 at 22:45
  • 1
    Does your input array have only a single item? Powershell "kindly" changes one-object arrays into just the contained object on the pipeline. I was about to make a comment about that, but then kittens started dying, so I stopped. – Paul Hicks Jul 07 '16 at 22:58
  • 2
    Cannot repro in psake. It may be a formatting issue in a complex psake script with various types of output. I have seen some similar effects in Invoke-Build. Try to add `Out-String`, i.e. `$json | ForEach-Object { $_ } | Out-String` – Roman Kuzmin Jul 07 '16 at 23:47

0 Answers0