Conversion from JSON failed with error: Unexpected character encountered while parsing value: @. Path '' nor quering it works

Hi, I am using Invoke-RestMethod to retreive dtails of repositories from Azur devops server as follows
$ListProject = (Invoke-RestMethod -Uri $OrgUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

Operation goes through, I can see the value of $ListProject as follows

count value
----- -----
   28 {@{objectId=b79db4fd12b3a0207fe1116ee8168e14b81d7d74; gitObjectType=tree; commitId=ee76cb8621e26604742bc5321ad3a3302bb22ddf; path=/; isFolder=True; url=https://azuredevops2k19.salam.net/Sierac-Utilities…

but when I try to query as json
$ListProject.value.[objectId, path]
I get

ParserError: Array index expression is missing or not valid

Trying to convert json to string or table using
$results = ConvertFrom-JSON $ListProject

I get the following error

ConvertFrom-Json: Conversion from JSON failed with error: Unexpected character encountered while parsing value: @. Path ‘’, line 0, position 0.

So how can I parse the returned json?

Invoke-RestMethod returns an array of objects, not strictly JSON. You can use Invoke-WebRequest and pass the resulting content or rawcontent to ConvertFrom-JSON

Thanks neemobeer, I did use Invoke-WebRequest

$ListProjectWebRequest = (Invoke-WebRequest -Uri $OrgUrl -Method Get -UseDefaultCredential -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)})

Then I convert as follows

$ListProjectWebRequestCJ = ConvertFrom-JSON $ListProjectWebRequest

when I run $ListProjectWebRequestCJ by itself, I get the exact same output as before , I am really dont know how I can parse this as I am not sure if it is json or string. I tried also using the flag -AsHashtable as follows

$ListProjectWebRequestCJHT = ConvertFrom-JSON $ListProjectWebRequest -AsHashtable

when I run $ListProjectWebRequestCJHT by itself it gives

Name Value


count 28
value {b79db4fd12b3a0207fe1116ee8168e14b81d7d74, 3e759b75bf455ac809d0987d369aab89137b5689, 5c8b41ed093ed2f58057f8ed6edb9bc4ac906a51, b77670a17a97a8cb61434496f43d8371f695991b…}

which seems contains only objectIds not other properties in the json file

Sometimes the data is nested when it’s returned. $ListProjectWebRequestCJ.value has the data you want

You are correct, I did that and it containes the details I need but is it a collection or an array?
I did the following and got some good results

$ListProjectWebRequestCJ.value | Select Id, name, lastUpdateTime, url | Format-Table

Another auestion pleqse . how can I find all properties to display as the properties I was able to use I found it in postman

You can always pass a variable (object) to Get-Member to see what type it is as well it’s property types. To find properties from an API you will have to consult the specific API documentation.

OK got it, I was able to play with the objects. Here is a piece of the returned values

“value”: [
{
“id”: “0d1c66ba-d842-4a7e-ae1a-10ef9e4279e9”,
“name”: “session-3-demo.git”,
“url”: “https://azuredevops2k19.salam.net/Sierac-Utilities/6e767fad-8a74-40d8-900a-3673b5be8049/_apis/git/repositories/0d1c66ba-d842-4a7e-ae1a-10ef9e4279e9”,
“project”: {
“id”: “6e767fad-8a74-40d8-900a-3673b5be8049”,
“name”: “dotNet Projects”,
“description”: “Group misc net projects”,
“state”: “wellFormed”,
“revision”: 75,
“visibility”: “private”,
“lastUpdateTime”: “2021-01-08T12:29:41.537Z”
},
“defaultBranch”: “refs/heads/main”,
“size”: 5374,
“remoteUrl”: “https://azuredevops2k19.salam.net/Sierac-Utilities/dotNet%20Projects/_git/session-3-demo.git”,
“isDisabled”: false
},

After transforming the returned value by Invoke-webrequest to hashtable, I can issue

$ListReposUrldotNetProjectsCJ.value | Select Id, name

and get right results, however when I try to access any propoerty in the project nested array I get nothing like this

$ListReposUrldotNetProjectsCJ.value | Select Id, name, project.id

I get nothing in the last outputfield field

Pretty sure your data is accessible like so

Correct, doing

$ListReposUrldotNetProjectsCJ.value[0].project

I get all properties for the nested project properties
also doing

$ListReposUrldotNetProjectsCJ.value[0].project.lastUpdateTime

I get the right value, however, when I issue

$ListReposUrldotNetProjectsCJ.value | Select Id, name, project.id

I get nothing under project.id

id name project.id


0d1c66ba-d842-4a7e-ae1a-10ef9e4279e9 session-3-demo.git
8766f993-1a06-4517-b11e-1db7eb6a599f BuildingMS-Play.Common
88dc7723-6900-420a-8bb6-22e2114f7e08 BuildingMS-Play.Inventory
19f1d481-7339-4251-887c-263462238e0c BuildingMS-Play.Catalog
b5b551eb-930f-476e-9d68-3a7aec9af8df BuildingMS-Play.Contracts
654a2259-21f3-4538-96b5-65774166dec9 Async.Await.Demo
7e8b26b1-3664-4225-a647-7de040f0e4fd UnitTesting-Julio
8901fcc5-a936-4ca5-ae01-96df213fd444 Session-3-Demo
1978d282-6a9a-497c-8706-a5b886628644 NET5_REST_API_Tutorial
3268c2c3-5d3c-44f7-8e68-a8b71391b586 Tech-Session-2
f5f6332d-ed07-403f-bc2e-c11274854dfb CommanderGQL
df2376b3-7229-4951-9384-ee972f5b431c dotNet Projects

when I issue
$ListReposUrldotNetProjectsCJ.value | Select Id, name, project
I get the whole json under project

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.