json path to get api value

1

How to get the low value of the following api :

[{"idreg":"257052","code":"USD","codein":"BRL","name":"D\u00f3lar Comercial",      
  "high":"3.4026","pctChange":"1.143","open":"0","bid":"3.3703","ask":"3.3713",  
  "timestamp":"1481828340000","low":"3.367","notFresh":"0","varBid":"0.0381",   
  "create_date":"2016-12-15 17:30:02"}] 

I do not quite understand what the path rules are for json but I tried the following way:

<?php    
    $json_file = file_get_contents("https://economia.awesomeapi.com.br/json/USD-BRL/1");   
    $dados = json_decode($json_file);    
    echo $dados->low;
?>

What am I doing wrong?

    
asked by anonymous 15.12.2016 / 20:45

2 answers

2
<?php

$json_file = file_get_contents("https://economia.awesomeapi.com.br/json/USD-BRL/1");   
$dados = json_decode($json_file);

echo $dados[0]->low;


?>
    
15.12.2016 / 20:53
2

You can do this:

$json_file = file_get_contents("https://economia.awesomeapi.com.br/json/USD-BRL/1");
$dados = json_decode($json_file);
echo $dados[0]->low; // 3.367

DEMONSTRATION

You can fetch the data as an array too:

...
$dados = json_decode($json_file, true);
echo $dados[0]['low']; // 3.367

Note that in data format:

Array
(
    [0] => Array
        (
            [idreg] => 257052
            [code] => USD
            [codein] => BRL
            [name] => Dólar Comercial
            [high] => 3.4026
            [pctChange] => 1.143
            [open] => 0
            [bid] => 3.3703
            [ask] => 3.3713
            [timestamp] => 1481828340000
            [low] => 3.367
            [notFresh] => 0
            [varBid] => 0.0381
            [create_date] => 2016-12-15 17:30:02
        )

)

These are all in key 0 of our main array, these are also an associative array if json_decode($json_file, true) otherwise they are an object by default json_decode($json_file) , and you should access $dados[0]->PROPRIEDADE_QUE_QUERES

    
15.12.2016 / 20:51