-4

I have a json object like below. I need to locate and load the "data" value in each variable for further processing. I went through few basic PHP examples here, but didn't work. Any help is greatly appreciated.

{
"dataset":
    {


    "id":9775409,
    "dataset_code":"AAPL",
    "database_code":"WIKI",
    "name":"Apple Inc (AAPL) Prices, Dividends, Splits and Trading Volume",
    "description":"End of day open, high, low, close and volume, dividends and splits, and split/dividend adjusted open, high, low close and volume for Apple Inc. (AAPL). Ex-Dividend is non-zero on ex-dividend dates. Split Ratio is 1 on non-split dates. Adjusted prices are calculated per CRSP (\u003ca href=\"http://www.crsp.com/products/documentation/crsp-calculations\" rel=\"nofollow\" target=\"blank\"\u003ewww.crsp.com/products/documentation/crsp-calculations\u003c/a\u003e)\r\n\r\n\u003cp\u003eThis data is in the public domain. You may copy, distribute, disseminate or include the data in other products for commercial and/or noncommercial purposes.\u003c/p\u003e\r\n\u003cp\u003eThis data is part of Quandl's Wiki initiative to get financial data permanently into the public domain. Quandl relies on users like you to flag errors and provide data where data is wrong or missing. Get involved: \u003ca href=\"mailto:connect@quandl.com\" rel=\"nofollow\" target=\"blank\"\u003econnect@quandl.com\u003c/a\u003e",
    "refreshed_at":"2017-11-03T21:50:44.247Z",
    "newest_available_date":"2017-11-03",
    "oldest_available_date":"1980-12-12",
    "column_names":["Date","Open","High","Low","Close","Volume","Ex-Dividend","Split Ratio","Adj. Open","Adj. High","Adj. Low","Adj. Close","Adj. Volume"],
    "frequency":"daily",
    "type":"Time Series",
    "premium":false,"
    limit":null,
    "transform":null,
    "column_index":null,
    "start_date":"2017-11-03",
    "end_date":"2017-11-03",
    "data":[["2017-11-03",174.0,174.26,171.12,172.5,58683826.0,0.0,1.0,174.0,174.26,171.12,172.5,58683826.0]],
    "collapse":null,
    "order":"asc",
    "database_id":4922}


}
Sam
  • 2,856
  • 3
  • 18
  • 29
Raja
  • 1
  • 1
  • 2
    Who is Jason? I don't think he was at the party.. in any case, try ["parse json php"](https://www.google.com/search?q=parse+json+PHP). Parsing JSON returns a PHP objects graph (eg. arrays, nested arrays, and string/number values). How is *that* dealt with *after* it is 'parsed'? – user2864740 Nov 04 '17 at 01:01
  • What kind of problems do You have with `json_decode($Jason)` ? – Michas Nov 04 '17 at 01:04
  • what's the source of your `json` source? – Sam Nov 04 '17 at 01:19
  • @Neodan that question is in `python` – Sam Nov 04 '17 at 01:23
  • @Samyel sorry, missclick. SO android app is very bad :-( – Neodan Nov 04 '17 at 01:25
  • @Neodan no problem man... I just use the Stack Exchange app... It's actually pretty good for Android – Sam Nov 04 '17 at 02:03
  • Here is the source https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json?start_date=2017-11-03&end_date=2017-11-3&order=asc&transformation=rdiff&api_key=xxxxxx – Raja Nov 04 '17 at 02:18

2 Answers2

0

You can treat a json object as you would with an array; you can loop through the json or just call individual files.

<?php
$json_source = ''; # your json source

# two parameters:
# 1st - the source of the json file
# 2nd - if set to true it will set the array as an associative array
$json = json_decode($json_resource, true);

print_r($json['database']['data']);

To read more about json_decode check out the manual.

This will output the json values from the data section. To assign value to each variable you can loop through the json:

foreach( $json['database']['data'] as $value )
{
    $data[] = $value;
}

This will create a new array called $data. You know would have something like this as the output:

Array
(
    [0] => 2017-11-03
    [1] => ...
)

Hope that helps!

Sam
  • 2,856
  • 3
  • 18
  • 29
  • Thanks . I made it little change and worked. But, I have a new problem. '; $name = $json['dataset']['name']; $str_pos = strpos($name,"("); $closing_price = $json['dataset']['data']; echo 'Name '.substr($name,0, $str_pos).'
    '; echo 'Closing price '.$closing_price[0][4].'
    '
    – Raja Nov 04 '17 at 20:02
  • The results is being cached now and it's take an hour or so to get the new values when change the symbol. The hosting company confirmed that there is no server side caching. – Raja Nov 04 '17 at 20:04
  • @Raja you can try to add a timestamp to your json file: `file_get_contents($url . '?' . date('Ymdhis');` OR you can add `no-cache` headers. For more look at this answer: https://stackoverflow.com/a/46890889/1527252 – Sam Nov 04 '17 at 20:27
  • Worked great. Thanks again Sam for your help! – Raja Nov 04 '17 at 23:17
0

Here is the final code:

<?php
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
   header("Cache-Control: post-check=0, pre-check=0", false);
   header("Pragma: no-cache");
  $url ='https://www.quandl.com/api/v3/datasets/WIKI/CNK.json?start_date=2017-11-03&end_date=2017-11-03&order=asc&transformation=rdiff&api_key=xxxx';
   $content = file_get_contents($url);
   $json = json_decode($content, true);
   $name = $json['dataset']['name'];
   $str_pos = strpos($name,"(");
   $closing_price = $json['dataset']['data'];
   echo 'Name '.substr($name,0, $str_pos).'<br/>';
   echo 'Closing price '.$closing_price[0][4].'<br/>';
?>
Raja
  • 1
  • 1