How to access nested JSON in PHP - php

I'm trying to access "note_id" in a JSON object.
php code
<?php
$test =
'{"username":"Ellaleeeeeee",
"event_source":"browser",
"event":
"{
\"notes\": [{\"note_id\": \"2771\"}]
}"
}';
$jarray = json_decode($test, true);
$jevent = json_decode($jarray['event'], true);
var_dump($jevent);
?>
I tried $jevent['notes']['note_id'][0] but failed.
Please kindly tell me what I am supposed to do. Thanks!

You need to change code a bit like below:-
$jarray = json_decode($test, true);
$jevent = json_decode($jarray['event'], true);
echo $jevent['notes'][0]['note_id'];
Output:-https://3v4l.org/0sOfm
In case you want to get all note_id then use foreach()
<?php
$test =
'{"username":"Ellaleeeeeee",
"event_source":"browser",
"event":
"{\"notes\": [{\"note_id\": \"2771\"},{\"note_id\": \"2772\"}]}"
}';
$jarray = json_decode($test, true);
$jevent = json_decode($jarray['event'], true);
foreach($jevent['notes'] as $jnotes){
echo $jnotes['note_id'].PHP_EOL;
}
Output:-https://3v4l.org/D7t0R

Related

Print PHP text from json

There is such a json from the Wikipedia API: https://ru.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=Apple
How to output text from php field in php where short text starts?
I tried like this, but nothing works:
$url = file_get_contents('https://ru.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=Apple');
$yummy = json_decode($url, true);
echo $yummy['extract'];
Do like this I debug that you exract in query > page > number
$url = file_get_contents('https://ru.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=Apple');
$yummy = json_decode($url, true);
$datas=$yummy['query']['pages'];
foreach($datas as $data){
$extract = $data['extract'];
}
echo $extract;
test is OK
$url = file_get_contents('https://ru.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=Apple');
$yummy = json_decode($url, true);
$yummys=$yummy['query']['pages'];
foreach($yummys as $yummy)
echo $yummy['extract'];

parse json from url using php and save in mysql db

I've a JSON response from some web service
{
"success":true,
"timestamp":1503291248,
"quotes":{
"SAD":"ABC",
"LOVE":"XYZ",
"FRIENDSHIP":"SOME",
"ENMITY":"LOREM IPSUM",
... //indicates there are a lot more categories
}
}
I have tried to echo data using this script
<?php
$url = 'MY_URL';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $idx=>$json ) {
echo $idx;
}
?>
this prints
successtermsprivacytimestampsourcequotes
but getting empty response, how can I echo/save above josn data in mySql?
<?php
$url = '{
"success":true,
"timestamp":1503291248,
"quotes":{
"SAD":"ABC",
"LOVE":"XYZ",
"FRIENDSHIP":"SOME",
"ENMITY":"LOREM IPSUM",
... //indicates there are a lot more categories
}
}';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $idx=>$json ) {
if(is_array($json))
{
foreach($json as $iidx=>$jjson)
{
echo $iidx.":".$jjson;
}
}
else
{
echo $idx.":".$json;
}
}
$ins_qry = 'INSERT INTO json_table(jsonvalues) values ("'.$json.'")';
$exec_qry = mysqli_query($link,$ins_qry);
?>
This will print json data.
To save this json data in mysql, you can directly insert the $json value into text column of a MySQL table.
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
Try this..
$url = 'MY_URL';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $val) {
echo $val['success']; //same for other keys
}

How to use PHP to fetch and decode JSON data?

How can I get variables "price_usd" and "price_btc" from JSON url https://api.coinmarketcap.com/v1/ticker/ethereum/? I wrote a script but nothing happens.
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Your code use a file_get_contents twice, look at would be:
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
echo $url;
//$json = file_get_contents($url);
$data = json_decode($tick, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Try this
<?php
$json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$data = json_decode($json);
var_dump($data[0]->price_usd);
?>

How to parse Json result?

Please help me get JSON result - "col1":"64.7020" to $result.
{"query":{"count":1,"created":"2016-08-28T09:50:07Z","lang":"ru-RU","diagnostics":{"publiclyCallable":"true","url":{"execution-start-time":"1","execution-stop-time":"2","execution-time":"1","content":"http://finance.yahoo.com/d/quotes.csv?e=.csv&f=c4l1&s=USDRUB=X"},"user-time":"2","service-time":"1","build-version":"0.2.48"},"results":{"row":{"col0":"RUB","col1":"64.7020"}}}}
$tick=file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDRUB%3DX%22%3B&format=json&diagnostics=true&callback=');
$url = $tick;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$result ??
echo col1 ???????
You have called file_get_contents twice. $tick will have the json returned by yahoo
$tick = file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDRUB%3DX%22%3B&format=json&diagnostics=true&callback=');
$json_a = json_decode($tick, TRUE);
echo $json_a["query"]["results"]["row"]["col1"];

How to get number of video views with YouTube API V3?

i use this code to achieve this but this don't work :
<?php
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$JSON_Data = json_decode($JSON);
$views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
Thanks
Try something like this:
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$json_data = json_decode($JSON, true);
echo $json_data['items'][0]['statistics']['viewCount'];

Categories