Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 376 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Invalid array passed" when parsing JSON

#1
I have this file which I want to read with PowerShell:

var myMap =
[
{
"name": "JSON Example",
"attr": "Another attribute"
}
]


My PowerShell v3 Code:

$str = Get-Content $file | Select -Skip 1;
$str | ConvertFrom-Json;
But I'm always getting this error:

ConvertFrom-Json : Invalid array passed in, ']' expected. (1): [
At S:\ome\Path\script.ps1:60 char:8
+ $str | ConvertFrom-Json;
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

If I copy and paste the JSON code manually into the code, everything is working fine:

'[
{
"name": "JSON Example",
"attr": "Another attribute"
}
]' | ConvertFrom-Json;
Reply

#2
Try to pipe to `Out-String` before piping to `ConvertFrom-Json`:

Get-Content $file | Select -Skip 1 | Out-String | ConvertFrom-Json


In your working example the JSON code is a string while the non-working example returns a collection of lines. Piping to `Out-String` converts the collection to a single string, which is what the InputObject parameter accept.

Reply

#3
Alternatively you can use `Get-Content -Raw` which will retrieve the JSON as a single string.

See this post for more info:

[To see links please register here]

Reply

#4
another answer that also works: use `GC -raw <FILE>` which will pass in as string
Reply

#5
The `var myMap =` isn't json, it's javascript. Delete the first line and it will be fine.

EDIT:

Oh, you are skipping the first line. It may be that there's a carriage return missing in the last line of the file, and Powershell 3 is more sensitive to it. It works fine in Powershell 5.1 even without a carriage return at the end.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through