I am trying to import/read the data from a file which is in array of hash table (key, value) but unable to do because the default assign sign (=) is not used instead colon (:) is used. I have tried different ways to read the data but powershell throws me an error every time. the data (truncated here) is in below format (notice the equal sign is replaced with :
[{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]
How can I read this data into array or variable, so I can work with it? This is the error message:
> @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]
At line:1 char:7
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~
Missing type name after '['.
At line:1 char:14
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~~~~~~~
Unexpected token ':"Alex"' in expression or statement.
At line:1 char:21
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~
Missing argument in parameter list.
At line:1 char:40
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~~~~~~~~
Unexpected token ':"Steve"' in expression or statement.
At line:1 char:48
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~
Missing argument in parameter list.
At line:1 char:67
+ ... ":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"9 ...
+ ~~~~~~~
Unexpected token ':"Lisa"' in expression or statement.
At line:1 char:74
+ ... :"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90 ...
+ ~
Missing argument in parameter list.
At line:1 char:93
+ ... "Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B ...
+ ~~~~~~~~~
Unexpected token ':"Jackie"' in expression or statement.
At line:1 char:102
+ ... Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B" ...
+ ~
Missing argument in parameter list.
At line:1 char:1
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~~
The splatting operator '@' cannot be used to reference variables in an expression. '@a' can be used only as an
argument to a command. To reference variables in an expression use '$a'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename
Your input appears to be JSON, so you need to:
first make it a PowerShell string[1], by enclosing it in '...'
(given that the value is to be used verbatim).
then parse the string into an object graph using the ConvertFrom-Json
cmdlet:
$arrayOfObjects = ConvertFrom-Json '[{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]'
$arrayOfObjects
then contains an array of [pscustomobject]
instances representing the JSON input; if you output $arrayOfObjects
, you'll see:
name IQ
---- --
Alex A+
Steve 60
Lisa 90
Jackie B
[1] The RHS of the assignment in your question (=
) starts with an unquoted [
, which means that PowerShell parses it in expression mode, and therefore expects [
to be part of a type literal, such as [string]
- see the conceptual about_Parsing help topic.
Also, note that sigil @
is never used in variable assignments (where $
must be used), only in the context of splatting; that is, use $var = ...
, never @var = ...
.