JSON and Charting

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I cannot wait till I fathom this since my next phase will be to learn couchdb

I am loving the Database side of things just the php language which struggles a bit still.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I still can't get this right I am running out of ideas

I just cannot get that price array

Trying to get property of non-object is a error I get a lot with this with my various attempts
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Not clear on what you can't get right; if it was to convert the javascript example, then here is the solution:

PHP:
<?php
	$input = '[
	            {
	                "dateTime": "2016-08-10T21:00:00Z",
	                "value": 13.2946
	            },
	            {
	                "dateTime": "2016-08-10T21:05:00Z",
	                "value": 13.2947
	            },
	            {
	                "dateTime": "2016-08-10T21:10:00Z",
	                "value": 13.2959
	            },
	            {
	                "dateTime": "2016-08-10T21:15:00Z",
	                "value": 13.2989
	            },
	            {
	                "dateTime": "2016-08-10T23:00:00Z",
	                "value": 13.3006
	            }
	        ]';

	function convert($v)
	{
	  return [strtotime($v->dateTime), $v->value];
	}

	$text = json_decode($input);
	date_default_timezone_set("UTC");
	$encoded = json_encode(array_map('convert', $text));
	print($encoded); 
?>

// returns:
Code:
[[1470862800,13.2946],[1470863100,13.2947],[1470863400,13.2959],[1470863700,13.2989],[1470870000,13.3006]]
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18131303 said:
PHP:
<?php
$input = '[
            {
                "dateTime": "2016-08-10T21:00:00Z",
                "value": 13.2946
            },
            {
                "dateTime": "2016-08-10T21:05:00Z",
                "value": 13.2947
            },
            {
                "dateTime": "2016-08-10T21:10:00Z",
                "value": 13.2959
            },
            {
                "dateTime": "2016-08-10T21:15:00Z",
                "value": 13.2989
            },
            {
                "dateTime": "2016-08-10T23:00:00Z",
                "value": 13.3006
            }
        ]';

function convert($v)
{
  return [strtotime($v->dateTime), $v->value];
}

$text = json_decode($input);
date_default_timezone_set("UTC");
$encoded = json_encode(array_map('convert', $text));
print($encoded); 
?>

But that is not what my input looks like...

I posted it a bit earlier in this thread, on phone will post the full input as I get it from the API in a moment when I get to the pc
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
The only bit I saw was a partial copy; I'll help if you link to the full JSON.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18131321 said:
The only bit I saw was a partial copy; I'll help if you link to the full JSON.

{"securityType":"CURRENCY","basicQuote":{"id":"usdzar:CUR","securityName":"US DOLLAR/SOUTH AFRICAN RAND","name":"USDZAR Spot Exchange Rate","primaryExchange":"","price":13.3819,"issuedCurrency":"ZAR","priceChange1Day":0.1005,"percentChange1Day":0.7567,"priceMinDecimals":4,"nyTradeStartTime":"17:00:00.000","nyTradeEndTime":"16:59:59.000","timeZoneOffset":-4,"lastUpdateEpoch":"1470940973","openPrice":13.2814,"lowPrice":13.2549,"highPrice":13.4051,"previousClosingPriceOneTradingDayAgo":13.2814,"lowPrice52Week":12.7238,"highPrice52Week":17.9169,"priceDate":"8/11/2016","priceTime":"2:42 PM","lastUpdateTime":"2:42 PM","lastUpdateISO":"2016-08-11T18:42:53.000Z","userTimeZone":"EDT"},"detailedQuote":{"id":"usdzar:CUR","openPrice":13.2814,"lowPrice":13.2549,"highPrice":13.4051,"previousClosingPriceOneTradingDayAgo":13.2814,"lowPrice52Week":12.7238,"highPrice52Week":17.9169,"totalReturnYtd":-13.4887,"priceMinDecimals":4,"userTimeZone":"EDT"},"marketStatus":{"ultimateParentTicker":null,"parentMarketStatus":null,"id":"usdzar:CUR","userTimeZone":"EDT"},"priceTimeSeries":[{"id":"usdzar:CUR","dateTimeRanges":{"start":"2016-08-10T21:00:00Z","end":"2016-08-11T20:59:00Z"},"price":[{"dateTime":"2016-08-10T21:00:00Z","value":13.2946},{"dateTime":"2016-08-10T21:05:00Z","value":13.2947},{"dateTime":"2016-08-10T21:10:00Z","value":13.2959},{"dateTime":"2016-08-10T21:15:00Z","value":13.2989},{"dateTime":"2016-08-10T21:20:00Z","value":13.2976},{"dateTime":"2016-08-10T21:25:00Z","value":13.2976},{"dateTime":"2016-08-10T21:30:00Z","value":13.2915},{"dateTime":"2016-08-10T21:35:00Z","value":13.2878},{"dateTime":"2016-08-10T21:40:00Z","value":13.2792},{"dateTime":"2016-08-10T21:45:00Z","value":13.2773},{"dateTime":"2016-08-10T21:50:00Z","value":13.2759},{"dateTime":"2016-08-10T21:55:00Z","value":13.2944},{"dateTime":"2016-08-10T22:00:00Z","value":13.293},{"dateTime":"2016-08-10T22:05:00Z","value":13.303},{"dateTime":"2016-08-10T22:10:00Z","value":13.2987},{"dateTime":"2016-08-10T22:15:00Z","value":13.2999},{"dateTime":"2016-08-10T22:20:00Z","value":13.2986},{"dateTime":"2016-08-10T22:25:00Z","value":13.2993},]}
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Paste it into into http://jsonlint.com/

The structure and hierarchy is obvious then.

I still can't get the price array I keep getting non object error

It's the structure and hierarchy that I'm not getting it would seem ie I don't know how to loop it to get the values of pricetimeseries -> price.
 

battletoad

Expert Member
Joined
Mar 10, 2009
Messages
1,451
I'm just skimming so this may be totally OT... you have a json string and you want an array in php?
Code:
  $arr = json_decode($jsonString, true);
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
PHP:
<?php
	$input = file_get_contents("http://www.bloomberg.com/markets/api/quote-page/usdzar:CUR");

	function convert($v)
	{
	  return [strtotime($v->dateTime), $v->value];
	}

	$text = json_decode($input);
	date_default_timezone_set("UTC");
	$encoded = json_encode(array_map('convert', $text->priceTimeSeries[0]->price));
	print($encoded); 
?>

As to storing the data in MYSQL; you have two options; store each time/price value as individual records; array_map & join can help you to easily construct the SQL insert command.
Alternatively store the entire unencoded string value in a column (a large one); in the above php code; this would be the $input variable.
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I'm just skimming so this may be totally OT... you have a json string and you want an array in php?
Code:
  $arr = json_decode($jsonString, true);

No.

I take some values from that JSON but I also require and array of the price time

That JSON is a bunch of nested arrays after a very specific array inside the array if that makes sense.
 

biometrics

Honorary Master
Joined
Aug 7, 2003
Messages
71,858
I still can't get the price array I keep getting non object error

It's the structure and hierarchy that I'm not getting it would seem ie I don't know how to loop it to get the values of pricetimeseries -> price.

Something like this? Untested ...

Code:
$bloomberg_array = json_decode($bloomberg_json, true);
foreach($bloomberg_array['priceTimeSeries']['price'] as $price) {
  echo $price['dateTime'] . '<br>';
  echo $price['value'] . '<br>';
}
 

battletoad

Expert Member
Joined
Mar 10, 2009
Messages
1,451
No.

I take some values from that JSON but I also require and array of the price time

That JSON is a bunch of nested arrays after a very specific array inside the array if that makes sense.

Droid has you covered, but if you want to go the json_decode + true option, prices can be easily gotten as an array with
Code:
  $arr = json_decode($jsonString, true);
  $prices = $arr['priceTimeSeries'][0]['price'];
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18131439 said:
PHP:
<?php
	$input = file_get_contents("http://www.bloomberg.com/markets/api/quote-page/usdzar:CUR");

	function convert($v)
	{
	  return [strtotime($v->dateTime), $v->value];
	}

	$text = json_decode($input);
	date_default_timezone_set("UTC");
	$encoded = json_encode(array_map('convert', $text->priceTimeSeries[0]->price));
	print($encoded); 
?>

As to storing the data in MYSQL; you have two options; store each time/price value as individual records; array_map & join can help you to easily construct the SQL insert command.
Alternatively store the entire unencoded string value in a column (a large one); in the above php code; this would be the $input variable.

I LOVE YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


PHP:
//Currency Updates

// set quotes 
$quotes = array("USDZAR", "EURZAR", "GBPZAR");

//Parse about any English textual datetime description into a Unix timestamp
function convert($v) 
    { 
      return [strtotime($v->dateTime), $v->value]; 
    }

//Start the loop
foreach ($quotes as $quote) {
    $url = "http://www.bloomberg.com/markets/api/quote-page/$quote:CUR";
    $json = file_get_contents($url);
    $json = json_decode($json); 
    $currency_name = $json->basicQuote->securityName;
    $currency_price = $json->basicQuote->price;
    $currency_percentage = round($percentage = $json->basicQuote->percentChange1Day, 2, PHP_ROUND_HALF_UP);
    $currency_history = json_encode(array_map('convert', $json->priceTimeSeries[0]->price));
    $modified_time = date("Y-m-d H:i:s");

  
    echo "Currency Name $currency_name";
    echo "<br>";
    echo "Currency Price $currency_price";
    echo "<br>";
    echo "Currency Change $currency_percentage";
    echo "<br>";
    echo $currency_history;
    echo "<br>"; 
  
}


2016-08-11_21-18-32.jpg
 
Top