I am running into a problem with my script. I keep getting this error:
Conversion from JSON failed with error:
Unexpected character encountered while parsing value: H.
Path '', line 0, position 0.
I have been looking for any information online but cant seem to find anything helpful. Im not able to share the json data coming through, but its just a big generic brick of data.
$headers = @{
Authorization = $bearerToken}
$param = @{
Method = "GET"
Uri = fooURL
ContentType = "application/json"}
$result = Invoke-WebRequest @param -Headers $headers
$result = $result | Select-Object -ExpandProperty RawContent | ConvertFrom-Json
foreach($key in $result.Resources.PSobject.Properties){
if($key.userName.Value.StartsWith($nrUserName)){
$id = $key.id.Value
}
}
$id
This is my simple little script, the error is coming from the last pipe (ConvertFrom-Json). Also another bit of info, this script does not work on my macOS machine but did work on my coworkers windows machine. Any help would be appreciated!
I originally just tried rewriting the script in hopes it was just something i messed up, but i also implemented a try/catch but i need the code to still run and it'll just error out and not give the results needed.
CodePudding user response:
The RawContent
property is the "Full response content, including the HTTP status line, headers, and body." So it starts with the status line which starts with the word HTTP
.
The error message you're getting says that the first character (line 0, position 0) is an H
, which is invalid. That's to be expected if you try to pass the RawContent through a JSON parser, because a JSON value cannot start with H
,
JSON values will normally start with a [
(if the value is an array) or a {
(if the value is an object). It's also legal for the value to be a number (which starts with a digit or a minus sign), or to be a string (which starts with a "
), or to be one of the special values true
, false
and null
, always written in lower-case. That makes a total of 17 possible first characters. H
is not on this list. (Technically, JSON streams can also include whitespace -- space, carriage return, linefeed or tab -- and PowerShell's ConvertFrom-Json
tool accepts comments starting with //
. But still no H
s.)
In short, RawContent
cannot be correct, and if you're actually getting a JSON object back from that request, you don't need -ExpandProperty
at all, because converting the response object to a string will give you exactly the string you want to feed into ConvertFrom-Json
(at least, as I understand it). But you should check to make sure you're actually getting real JSON back from the request.
Note that using the -ContentType
option in the request is pointless, because that option is used to describe the content sent with the request. GET
requests don't have any content; the request is fully described by the headers. -ContentType
should be used with POST
and PUT
requests, since there is almost always content sent with them. If you need to tell the remote server that you want JSON (and it doesn't just assume that from the extension at the end of the URL), you should create an Accept
header with the value "application/json"
.
But before you try any of that, start by looking at what is actually being returned by the webserver. Is it JSON? Does it contain JSON embedded in something? Or it is in a completely different format?