0

this has been annoying me for a while especially as I have solved similar problems in the past but can't find any reference. I want to search a list of objects for a particular property, the property is the coin name:

$URL = "https://www.cryptocompare.com/api/data/coinlist/"
$WebRequest = Invoke-WebRequest $URL | ConvertFrom-Json
foreach ($coin in $WebRequest.Data)
{
$coin.FullName -match "Bitcoin (BTC)"
Write-Host "Found It!"
}

That's a simple version of my problem, the objects in .data property I can't seem to enumerate an iterate through like I would normally? Is there a better way to find an object in a list of objects from some converted JSON?

Thanks

Steve

3
  • The issue is what is being stored in $webrequest: @{BTC=; LTC=; DASH=; its missing the actual name from the site. Commented Jan 23, 2018 at 1:51
  • It would be helpful if you added some code illustrating you mean by "...to enumerate an iterate through like I would normally" Commented Jan 23, 2018 at 2:30
  • The JSON object returned doesn't let you iterate over it in that way. You have to navigate by the "Symbol" e.g. $WebRequest.Data.BTC. The data is of type NoteProperty, with very few methods left to tinker with: $WebRequest.Data | Get-Member | Where-Object MemberType -ne NoteProperty Commented Jan 23, 2018 at 8:11

1 Answer 1

2

That is because there is no .FullName property relative to what you are calling. There actually are no field headers at all.

So, you'll need to create the field header are not use that .property and just ask for the string.

FullName in the data set is just a string with the rest of the data in the Data property array.

$URL = "https://www.cryptocompare.com/api/data/coinlist/"
$WebRequest = Invoke-WebRequest $URL | ConvertFrom-Json

$WebRequest | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name             MemberType   Definition                                                                                                                                       
----             ----------   ----------                                                                                                                                       
Equals           Method       bool Equals(System.Object obj)                                                                                                                   
GetHashCode      Method       int GetHashCode()                                                                                                                                
GetType          Method       type GetType()                                                                                                                                   
ToString         Method       string ToString()                                                                                                                                
BaseImageUrl     NoteProperty string BaseImageUrl=https://www.cryptocompare.com                                                                                                
BaseLinkUrl      NoteProperty string BaseLinkUrl=https://www.cryptocompare.com                                                                                                 
Data             NoteProperty System.Management.Automation.PSCustomObject Data=@{BTC=; LTC=; DASH=; XMR=; NXT=; ETC=; DOGE=; ZEC=; BTS=; DGB=; BTCD=; PPC=; CRAIG=; XBS=; XP...
DefaultWatchlist NoteProperty System.Management.Automation.PSCustomObject DefaultWatchlist=@{CoinIs=1182,7605,5038,24854,3807,3808,202330,5324,5031,20131; Sponsored=}         
Message          NoteProperty string Message=Coin list succesfully returned! This api is moving to https://min-api.cryptocompare.com/data/all/coinlist, please change the path.
Response         NoteProperty string Response=Success                                                                                                                          
Type             NoteProperty int Type=100    

So, try something like this...

$TempFile = [System.IO.Path]::GetTempFileName()
Test-Path -Path $TempFile
$WebRequest.Data | Out-File $TempFile
# psedit $TempFile
$CoinReport = Import-Csv -Path $TempFile -Delimiter ':' -Header Coin,FullName
# $CoinReport | Format-Table -AutoSize -Wrap
$CoinReport | Select FullName | Where FullName -Like '*Bitcoin (BTC)*'

Results

True

FullName                                                                                                                                             
--------                                                                                                                                             
@{Id=1182; Url=/coins/btc/overview; ImageUrl=/media/19633/btc.png; Name=BTC; Symbol=BTC; CoinName=Bitcoin; FullName=Bitcoin (BTC); Algorithm=SHA256; 

Or after looking at this again, just do this, since the data you are looking for is in an array already.

$WebRequest.Data | % {$_.BTC} | Format-Table -AutoSize -Wrap

Results


Id   Url                 ImageUrl             Name Symbol CoinName FullName      Algorithm ProofType FullyPremined
--   ---                 --------             ---- ------ -------- --------      --------- --------- -------------
1182 /coins/btc/overview /media/19633/btc.png BTC  BTC    Bitcoin  Bitcoin (BTC) SHA256    PoW       0   


$WebRequest.Data | % {$_.BTC.FullName} | Format-Table -AutoSize -Wrap

Results

Bitcoin (BTC)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks all I will have a try sorry about the lack of detail it was late! I am using this API as I have a set of wallet balances im pulling into a nice table and I want to ge the short name of it so I can then do another lookup and get the exchange rate and I can have a GBP column in my table. So I need to search through that object locate the currency with the same name and pull the short code.....
The problem is the second example is that the matches aren't going to be exact so I need to find a away of doing a -like or similar but using the 2nd example as I want to try and avoid using files if I can...…..
Here' s you issue though. JSON is just one long string, though JSON beautifiers clean those up for humans. Meaning making them look like table records. As noted by my 2nd example and s noted by gvee, you really only have two choices. Navigate by symbol, or use my first example with the file. Now that sample jest create and new tempfile each time, but you can change that to create just one, that you overwrite each time. So, then it's just a file and not files(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.