To manipulate a JSON, you first need to know your syntax. When JSON is delimited by keys ({
and }
), it corresponds to a JSON Object, which is a set of several pairs "key/value".
For example:
{
"nome": "Fulano",
"idade": 25
}
This is a JSON Object who holds the key nome
, whose value is the string "Fulano"
, and the key idade
, whose value is the number 25
.
The detail is that values can be different Objects. Ex:
{
"nome": "Fulano",
"idade": 25,
"contatos": {
"telefone": "(11) 91234-5678",
"email": "[email protected]"
}
}
In this case, the value corresponding to the key contatos
is another Object, which in turn has two keys: telefone
and email
, whose values are, respectively, the strings "(11) 91234-5678"
and "[email protected]"
.
So the JSON you’re manipulating is a Object, who owns Objects within other Objects and so on.
In PHP, when you use the function json_decode
with the second parameter equal to true
, return is an associative array. So let’s print the array corresponding to your JSON:
$str = '{"by":"default","valid_key":true,"results":{"currencies":{"source":"BRL","USD":{"name":"Dollar","buy":3.8732,"sell":null,"variation":-1.078},"EUR":{"name":"Euro","buy":4.3412,"sell":null,"variation":-1.46},"GBP":{"name":"Pound Sterling","buy":5.0878,"sell":null,"variation":-0.611},"ARS":{"name":"Argentine Peso","buy":0.0905,"sell":null,"variation":-0.11},"BTC":{"name":"Bitcoin","buy":17082.794,"sell":17082.794,"variation":1.445}},"stocks":{"IBOVESPA":{"name":"BM\u0026F BOVESPA","location":"Sao Paulo, Brazil","points":96538.117,"variation":1.18},"NASDAQ":{"name":"NASDAQ Stock Market","location":"New York City, United States","points":7796.71,"variation":0.87},"CAC":{"name":"CAC 40","location":"Paris, French","variation":1.03},"NIKKEI":{"name":"Nikkei 225","location":"Tokyo, Japan","variation":1.43}},"available_sources":["BRL"],"bitcoin":{"blockchain_info":{"name":"Blockchain.info","format":["USD","en_US"],"last":4160.86,"buy":4160.86,"sell":4160.86,"variation":1.445},"coinbase":{"name":"Coinbase","format":["USD","en_US"],"last":4142.7,"variation":1.537},"bitstamp":{"name":"BitStamp","format":["USD","en_US"],"last":4142.95,"buy":4142.83,"sell":4141.33,"variation":1.698},"foxbit":{"name":"FoxBit","format":["BRL","pt_BR"],"last":16745.68,"variation":2.72},"mercadobitcoin":{"name":"Mercado Bitcoin","format":["BRL","pt_BR"],"last":16489.95994,"buy":16400.0,"sell":16489.95994,"variation":1.227},"omnitrade":{"name":"OmniTrade","format":["BRL","pt_BR"],"last":16250.0,"buy":16250.0,"sell":16610.0,"variation":0.994},"xdex":{"name":"XDEX","format":["BRL","pt_BR"],"last":16299.0,"variation":1.154}},"taxes":[{"date":"2019-03-29","cdi":6.4,"selic":6.4,"daily_factor":1.0002462}]},"execution_time":0.0,"from_cache":true}';
$obj = json_decode($str, true);
var_dump($obj);
The output is (omitted several lines for brevity reasons):
array(5) {
["by"]=>
string(7) "default"
["valid_key"]=>
bool(true)
["results"]=>
array(5) {
["currencies"]=>
array(6) {
["source"]=>
string(3) "BRL"
["USD"]=>
array(4) {
["name"]=>
string(6) "Dollar"
["buy"]=>
float(3.8732)
["sell"]=>
NULL
["variation"]=>
float(-1.078)
}
.....
Notice that the information you want is in the key buy
of an array, which in turn is in the key USD
of another array, which in turn is in the key currencies
of another array, which in turn is in the key results
array.
We can see this more clearly if we format your JSON (also with several lines omitted and some comments added by me):
{
"by": "default",
"valid_key": true,
"results": { <-- chave "results", valor é outro object
"currencies": { <-- chave "currencies", valor é outro object
"source": "BRL",
"USD": { <-- chave "USD", valor é outro object
"name": "Dollar",
"buy": 3.8732, <-- chave "buy", valor que você quer
"sell": null,
"variation": -1.078
},
.....
So to get this value, just go through the array using the correct keys. If I do $obj['results']
, I’ll get the Object key matching results
(which is the object that holds the key currencies
).
Then with $obj['results']['currencies']
I get the Object key matching currencies
, whose value is the Object who holds the key USD
, and so on, until we get to the final code:
print($obj['results']['currencies']['USD']['buy']); // 3.8732