Link To JSON File: https://pastebin.com/gXcnNnUK
Code I've Tried So Far:
$json = json_decode($json, true);
foreach($json as $data){
echo $data['videoId'];
}
But it's not returning values. Is there any better approach to do it?
On line 3, did you mean to iterate over $arr instead of $json?
$json = file_get_contents('json.json');
$arr = json_decode($json, true);
foreach($arr['contents']['sectionListRenderer']['contents']['2']['itemSectionRenderer']['contents'] as $pero){
$video_id_list .= $pero['compactVideoRenderer']['videoId'].',';
}
I have JSON data:
{"took":3,
"timed_out":false,
"_shards":{ "total":5,
"successful":5,
"failed":0
},
"hits":{"total":105,
"max_score":1.0,
"hits":[
{"_index":"csv",
"_type":"logs",
"_id":"AVeyr-BQodQ9UhW1sdW0",
"_score":1.0,
"_source":{"message":"james,wonder\r",
"#version":"1",
"#timestamp":"2016-10-11T07:40:52.332Z",
"path":"D:\\logstash-2.4.0\\bin\\Book1.csv",
"host":"CHNL-LT-544",
"fname":"fa1368a93aa39b2346329c1734be1f4b",
"lname":"wonder"
}
},
{"_index":"csv",
"_type":"logs",
"_id":"AVeyr-BQodQ9UhW1sdW1",
"_score":1.0,
"_source":{"message":"muskaan, wonder\r",
"#version":"1",
"#timestamp":"2016-10-11T07:40:52.332Z",
"path":"D:\\logstash-2.4.0\\bin\\Book1.csv",
"host":"CHNL-LT-544",
"fname":"479085e94d305129527fa80978613e95",
"lname":"wonder"}
},
{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdW3","_score":1.0,"_source":{"message":"altimetrik,india\r","#version":"1","#timestamp":"2016-10-11T07:40:52.333Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"8b3ecf275832c79b36d62d74f47257c9","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdW8","_score":1.0,"_source":{"message":"kool,indiae\r","#version":"1","#timestamp":"2016-10-11T07:40:52.335Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"d2edbdce885720c70f38f36748c37600","lname":"indiae"}},{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdXA","_score":1.0,"_source":{"message":"ds,dssd\r","#version":"1","#timestamp":"2016-10-11T07:40:52.336Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"91594a75bc6bfb0ec800d4f454e1fe87","lname":"dssd"}},{"_index":"csv","_type":"logs","_id":"AVeytJ0iodQ9UhW1sdXf","_score":1.0,"_source":{"message":"kool,india\r","#version":"1","#timestamp":"2016-10-11T07:46:02.839Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"d2edbdce885720c70f38f36748c37600","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytJ0iodQ9UhW1sdXi","_score":1.0,"_source":{"message":"karnataka,india\r","#version":"1","#timestamp":"2016-10-11T07:46:02.840Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"2a0785caed6bfd2ffb6ad5a449f2bd0e","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdXq","_score":1.0,"_source":{"message":"james,wonder\r","#version":"1","#timestamp":"2016-10-11T07:48:39.550Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"fa1368a93aa39b2346329c1734be1f4b","lname":"wonder"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdX0","_score":1.0,"_source":{"message":"rajasthan,india\r","#version":"1","#timestamp":"2016-10-11T07:48:39.558Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"449b07d9038569b2d69243dadfae0371","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdX5","_score":1.0,"_source":{"message":"najeeb,shariff\r","#version":"1","#timestamp":"2016-10-11T07:48:39.573Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"be7a5e98258338fa63086f44e8bd1850","lname":"shariff"}}]}}
I want to print only 2 fields from this JSON; fname and lname. Please tell me how I can achieve this using PHP.
$url = 'localhost:9200/csv/_search';;
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['hits'] as $item) {
print $item['fname'];
}
But getting error Undefined index
$url = 'localhost:9200/csv/_search';;
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['hits']['hits'] as $item) {
echo "<p>{$item['_source']['fname']}</p>";
echo "<p>{$item['_source']['lname']}</p>";
}
Array path of fname and lname goes like [hits][hits][0][_source][fname] and [hits][hits][0][_source][lname]. [hits][hits] should be iterated in the loop to access each _source element.
As RiggsFolly said, you need to do print_r($json) to see how the array is structured especially when you encounter nested arrays.
Hope it helps!
I'm on PHP and I need to edit a JSON output to return only objects >=0 and divided by one hundred
Eg.
$json = {"data":[0,55,78,-32,-46,37]}
Needed
$json = {"data":[0,0.55,0.78,0.37]}
How this can be done?
Well, I know this is not the best practice, but if it's as simple as this, you can do the following.
$json = '{"data":[0,55,78,-32,-46,37]}';
// decoding the string to objects & arrays
$x = json_decode($json);
// applying a function on each value of the array
$x->data = array_map(
function($a)
{
if( $a >= 0 ) return $a/100;
else return null;
},
$x->data
);
// Removing empty values of the array
$x->data = array_filter($x->data);
// making a JSON array
$jsonData = json_encode(array_values($x->data));
// inserting a JSON array in a JSON Object
$json = '{"data":' . $jsonData . '}';
// here is your {"data":[0,0.55,0.78,0.37]}
echo $json;
Hope it helps !
Btw, I had to trick the json encode with array_values to prevent the creation of an object rather than an array for the data content. But I guess there is a better method that I just don't know ...
EDIT :
Find out the way :D
Once empty values removed from the array, just do :
$x->data = array_values($x->data);
$json = json_encode($x);
This will do the trick and it will not create issues with the rest of the object.
Alessandro:
Here's my approach, feel free to try. json_decode and a simple foreach can help you...
Code:
$json = array();
$result = array();
$json = '{"data":[0,55,78,-32,-46,37]}';
$decoded_json=json_decode($json, TRUE);
foreach ($decoded_json['data'] as &$value) {
if ($value >= 0){
$value = $value / 100;
$result[]=$value;
}
}
echo json_encode($result);
?>
Result:
[0,
0.55,
0.78,
0.37
]
I try to put a JSON Object into a PHP Variable, it is not working. When I var_dump it, it says NULL.
PHP:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$money = $data->tplc->stats; // -> What do i have to do to reach the id:GTA Online Cash and use the val: $33.9K ?
?>
JSON file example: (it changes over time)
{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"$33.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}
Could anyone also help me with what I've mentioned in the PHP file?
tplc isn't a JSON object, it's a string that is itself JSON.
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>
To get the money value specifically:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
if ($stat->id == 'GTA Online Cash') {
$money = $stat->val;
break;
}
}
echo $money;
?>
I have tried many different variations here - how can I get the content summary from this: http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c
Here's what I have so far:
$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);
I know I need to do something like echo $data[0]->query but am unable to get it to do anything.
$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);
foreach ($data['query']['pages'] as $page) {
print_r($page['revisions'][0]['*']);
}