How to read the values in json if there is a dash(-) in keys using powershell

Viewed 800

Given this Json:

{
 “StudentInfo":{
   “first-name": “xyz",
   “Student_id": "123-xyz"
 }
}

I could read the value for student_id using the code below.

$config = Get-Content -Raw $Config
$configObject = ConvertFrom-Json –InputObject $config
$StudentId = $configObject.StudentInfo.Student_id

However, I am unable to read the value for "first-name" as powershell is treating it to be a command instead of variable.Changing key name is not an option for me.

How can we retrieve the value for "first-name" using powershell 5.1?

1 Answers

use quotes:

$configObject.StudentInfo.'first-name'
Related