rogains
September 24, 2020, 3:06am
1
Hy,
I’m discovering Powershell, I would like to use value from a JSON file but no way to convert them into Real or float or int…
So I can’t use them.
I’m noob please help
This is my try :
$base_url = "https://api.minerstat.com/v2/coins?list=BTC,ETH,ETC"
Start-Sleep -Seconds 1
$URL = "$($base_url)"
$WebRequest = Invoke-WebRequest $URL | ConvertFrom-Json
$WebRequest | where{$_.coin -eq "ETH"} | select -expandProperty reward -OutVariable ETH_reward
$WebRequest | where{$_.coin -eq "ETH"} | select -expandProperty price -OutVariable ETH_price
$ETH_proffit = (Get-Variable ETH_price) * (Get-Variable ETH_reward)
Thx.
There are many ways to do perform casts. When you get data from the API, it’s a string. Powershell will guess and try to do conversions before math operations, but you can also force the casting with types. Using Invoke-RestMethod will automatically convert from JSON to a PSObject. Once you’ve done that, you can use Select object to do conversions and even add a property with the completed math.
$base_url = "https://api.minerstat.com/v2/coins?list=BTC,ETH,ETC"
$WebRequest = Invoke-RestMethod $base_url -Method GET
$WebRequest | Select-Object -Property id,
coin,
name,
type,
algorithm,
network_hashrate,
difficulty,
@{Name='reward';Expression={[float]$_.reward}},
reward_unit,
reward_block,
@{Name='price';Expression={[float]$_.price}},
volume,
@{Name='updated';Expression={[int]$_.updated}},
@{Name='total_reward';Expression={[float]$_.price * [float]$_.reward}}
Output:
id : a195fd59ce0ebc3f9b2d99b3c396ff198bcb4a5e
coin : BTC
name : Bitcoin
type : coin
algorithm : SHA-256
network_hashrate : 1.4306037987005E+20
difficulty : 19314656404097
reward : 2.712287E-19
reward_unit : BTC
reward_block : 6.25
price : 10408.54
volume : 15232458587.716
updated : 1600955721
total_reward : 2.82309449329572E-15
id : 31e1e1bcf3417b37586b0801d7a98d2346c9f30e
coin : ETH
name : Ethereum
type : coin
algorithm : Ethash
network_hashrate : 248670891285374
difficulty : 3176475226008931
reward : 3.684173E-12
reward_unit : ETH
reward_block : 3.2507453098605
price : 336.9015
volume : 12646101471.559
updated : 1600955793
total_reward : 1.24120340146503E-09
id : 4a31c231a1d2ef1664497590a29a58e12888a1b7
coin : ETC
name : Ethereum Classic
type : coin
algorithm : Ethash
network_hashrate : 3520202169646
difficulty : 44794572608758
reward : 2.57174E-10
reward_unit : ETC
reward_block : 3.2
price : 4.954754
volume : 318580826.05362
updated : 1600955393
total_reward : 1.27423401724484E-09
rogains
September 24, 2020, 6:50am
3
Thank you for answer.
I’m sorry but I don’t understand how to use your code.
I would like to store the result of of (reward) X (price) for coin:ETH
I try this but no success (no output) :
$base_url = “https://api.minerstat.com/v2/coins?list=BTC,ETH,ETC ”
$WebRequest = Invoke-RestMethod $base_url -Method GET
$ETH_price = $WebRequest | where{$.coin -eq “ETH”} | Select-Object -Property @{Name=‘price’;Expression={[float]$ .price}}
$ETH_reward = $WebRequest | where{$.coin -eq “ETH”} | Select-Object -Property @{Name=‘reward’;Expression={[float]$ .reward}}
$ETH_proffit = ($ETH_price) * ($ETH_reward)
$ETH_proffit
Thx.
rogains
September 24, 2020, 11:44am
4
After many try no way… I do not find the good syntax for doing what I want.
Your code work’s well and print the result as “total_reward ”
But I didn’t find the way to store this result per coin in different variables like $ETH_Pofit
I tried with | where{$_.coin -eq “ETH” } and many other things but no way.
I simply would like compare profits per coin in order to start the good program.
Thx.