arraysjsonpowershellconvertfrom-json

Powershell: Issues converting curl GET response to array


I'm unable to convert a GET response received with curl in Powershell to an array using ConvertFrom-Json. The request is pulling a list of users from a webapp. A couple JSON validators say the JSON is valid (FWIW). The results look like the data being converted from JSON is being crammed into one element in an array.

Sample JSON:

{"adRecordsSize":0,"azureRecordSize":0,"manualRecordsSize":76,"records":[{"id":"0-0","key":"someone","position":0,"sourceType":"Manual"},{"id":"0-1","key":"someone1","position":1,"sourceType":"Manual"},{"id":"0-2","key":"someone2","position":2,"sourceType":"Manual"},{"id":"0-3","key":"someone3","position":3,"sourceType":"Manual"}],"tisRecordsSize":0}

The results I'm looking for are an array I can work with, something like this (really all I need is the usernames):

id  key  position  sourceType
0-0  someone  0  Manual
0-1  someone1  1  Manual
0-2  someone2  2  Manual
0-3  someone3  3  Manual

Here's the first code I tried, which at least produces something:

$request = @()
$request= curl.exe -H "Csrf-Token:nocheck" -H $token -H "Content-Type: application/json" -H "Accept: application/json" $uri | ConvertFrom-Json
$request

Results below look like everything is crammed into one element or property (forgive my lack of terminology), but I was expecting an array containing an element for each item in the table I'm pulling from. Or is this multiple elements that ConvertFrom-Json isn't splitting up as it should?:

adRecordsSize     : 0
azureRecordSize   : 0
manualRecordsSize : 3
records           : {@{id=0-0; key=someone; position=0; sourceType=Manual}, @{id=0-1; key=someone1; position=1; sourceType=Manual}, @{id=0-2; key=someone2; position=2; sourceType=Manual}, @{id=0-3; key=someone3; position=3; 
                    sourceType=Manual}...}
tisRecordsSize    : 0

So instead I try:

$request = curl.exe -H "Csrf-Token:nocheck" -H $token -H "Content-Type: application/json" -H "Accept: application/json" $uri | Select-Object -Property records | ConvertFrom-Json

That results in an empty array. I change the select-object command to "Select-Object -Property *" in the pipeline: Same empty array.

So now I try this:

$request = curl.exe -H "Csrf-Token:nocheck" -H $token -H "Content-Type: application/json" -H "Accept: application/json" $uri
$data = ConvertFrom-Json -InputObject $request 
$data

The results are the same as the first step I tried!

So now this:

$request = [System.Collections.ArrayList]@()
$request = curl.exe --request GET -H "Csrf-Token:nocheck" -H $token -H "Content-Type: application/json" -H "Accept: application/json" $uri | Select-Object -Property * | ConvertFrom-Json
$request

Results in: "ConvertFrom-Json : Invalid JSON primitive: ."

I've tried a few other permutations, including using Out-String before ConvertFrom-Json, but with no success. Does anyone have an idea on how to solve this??


Solution

  • As mentioned in the comments, this is simply PowerShell trying to be helpful by showing a compact string representation of the more complex objects acturally stored in the records property.

    If you let it display the records value alone, you'll see exactly what you're looking for:

    PS ~> $request.records
    
    id  key      position sourceType
    --  ---      -------- ----------
    0-0 someone         0 Manual
    0-1 someone1        1 Manual
    0-2 someone2        2 Manual
    0-3 someone3        3 Manual
    
    
    PS ~> $request.records |Format-List
    
    
    id         : 0-0
    key        : someone
    position   : 0
    sourceType : Manual
    
    id         : 0-1
    key        : someone1
    position   : 1
    sourceType : Manual
    
    id         : 0-2
    key        : someone2
    position   : 2
    sourceType : Manual
    
    id         : 0-3
    key        : someone3
    position   : 3
    sourceType : Manual