JSON and Charting

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Now on to the next phase! :)

I would like to create a chart using data I have access to via a JSON API or not sure what the term here is the API spits out the data in JSON if that makes more sense.

I would like to use a specific Charting library, but they use the data is this form:

Code:
?([
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549],
[Date.UTC(2013,5,7),0.7562],
[Date.UTC(2013,5,9),0.7574],
[Date.UTC(2013,5,10),0.7543],
[Date.UTC(2013,5,11),0.7510],
[Date.UTC(2013,5,12),0.7498],
[Date.UTC(2013,5,13),0.7477],
[Date.UTC(2013,5,14),0.7492],
[Date.UTC(2013,5,16),0.7487],
[Date.UTC(2013,5,17),0.7480],
[Date.UTC(2013,5,18),0.7466]
]);

I have my data in this form (This is how I get it from the API I have access to.)

Code:
"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-10T23:00:00Z",
                "value": 13.3006
            }

        ],

So now my question is, is it possible to "convert" my data into the format the charting library would like?

I was thinking of using a for loop and string replace with/in php, but that is just my noob thinking so I guess my question is; How do I convert JSON data from one format to another?
 

Necropolis

Executive Member
Joined
Feb 26, 2007
Messages
8,401
Just write a function to transform the JSON from the format you are getting to the format you need - how hard could that be?
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
My thinking went something like this

Inside a for loop:

Get the API data using php

Code:
"dateTime": "2016-08-10T23:00:00Z",
"value": 13.3006

$date = "2016-08-10T23:00:00Z";
$price = "13.3006"

then convert date so that it displays "Date.UTC(2013,5,2)"

then insert into DB as Date and Price

then get the new JSON format from the DB
PHP:
<?php
$con = mysqli_connect("localhost","root","");

if (!$con) {
  die('Could not connect: ' . mysqli_error());
}

mysqli_select_db("stock_name", $con);

$result = mysqli_query("SELECT date, price FROM stock_name");

$rows = array();
while($r = mysqli_fetch_array($result)) {
    $row[0] = $r[0];
    $row[1] = $r[1];
    array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

mysqli_close($con);
?>

Save that to data.php

then in highcharts

HTML:
$.getJSON("data.php", function(json) {
    options.series[0].data = json;
    chart = new Highcharts.Chart(options);
});


However I am still lost I am babbling here.
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Just write a function to transform the JSON from the format you are getting to the format you need - how hard could that be?

Yea, easy for you to say. I am trying to the best of my limited ability.

Your signature sums me up:

"unaware-of-a-better-way code"
 

Necropolis

Executive Member
Joined
Feb 26, 2007
Messages
8,401
Yea, easy for you to say. I am trying to the best of my limited ability.

Your signature sums me up:

"unaware-of-a-better-way code"

You method of iterating over the JSON pulling out the values - transforming them and then creating a new JSON string is exactly what I would do if faced with a similar situation.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
You method of iterating over the JSON pulling out the values - transforming them and then creating a new JSON string is exactly what I would do if faced with a similar situation.

Thank god I am not that noob in my thinking then, just my skills to perform the actual action.
 

Spacerat

Expert Member
Joined
Jul 29, 2015
Messages
1,328
I would rather do the transformation server-side to return JSON more compatible with the consumer i.e. web page. That is if the server is under your control.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Thank god I am not that noob in my thinking then, just my skills to perform the actual action.
Boyd's Law of Iteration:
In analyzing complexity, fast iteration almost always produces better results than in-depth analysis.
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
I know little about php, but maybe you can loop over the json, and build up a string (using string concatenation) in the format you need. You can probably use a custom date formatter to print the dates in that format: http://php.net/manual/en/datetime.format.php and then append it with the rest of the data as you build up the string.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18128263 said:
Boyd's Law of Iteration:
In analyzing complexity, fast iteration almost always produces better results than in-depth analysis.

I am not on that level op quoting philosophical sayings yet.

My brain hurts, all I can come up with is:

that I need to find a way to convert

this:
dateTime": "2016-08-10T21:15:00Z

into this

Date.UTC(Y,M,DD)


example:

Date.UTC(2013,1,27)
Tue Feb 26 2013 19:00:00 GMT-0500 (Eastern Standard Time)
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Wait a minute!

It seems "Date.UTC()"

is a javascript function! So I can make it a string!!

Progress!!!!

I might be able to:

PHP:
<?php

$given = new DateTime("2014-12-12 14:18:00");
$a= $given->format("Y-m-d") . "\n";
echo "Date.UTC($a)"
?>

Output:
Code:
Date.UTC(2014-12-12 )

Now I just need to find a way to make the "-" into ","

EDIT: Well color me purple

PHP:
<?php

$given = new DateTime("2014-12-12 14:18:00");
$a= $given->format("Y,m,d") . "\n";
echo "Date.UTC($a)"
?>

Output:
Code:
Date.UTC(2014,12,12 )
 
Last edited:

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
You should be able to pull the whole shebang, including the "Date.UTC(" bit into the format statement.
$a= $given->format("Date.UTC(Y,m,d)")
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
You should be able to pull the whole shebang, including the "Date.UTC(" bit into the format statement.
$a= $given->format("Date.UTC(Y,m,d)")

PHP:
<?php

$given = new DateTime("2014-12-12 14:18:00");
$a= $given->format("Date.UTC(Y,m,d)");
echo $a
?>

Output:

Code:
Fripm31UTC.1418393880UTCC(2014,12,12)
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
All good, we made progress I got this sorted now.

What I am trying to do now is figuring out how to save an array in php?

PHP:
$currency_history = $json->priceTimeSeries->price;

I know the above won't work I am just trying to explain.

So, price is an array like so:
Code:
"price": [
		{
		    "dateTime": "2016-08-10T21:00:00Z",
		    "value": 13.2946
		},
		{
		    "dateTime": "2016-08-10T21:05:00Z",
		    "value": 13.2947
	        }
            ]

I tried

PHP:
$currency_history = $json->priceTimeSeries->array[price];

and

PHP:
$currency_history = $json->priceTimeSeries->price->array[];

But both gives me:

Fatal error: Cannot use [] for reading
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Are you using json_encode?
http://php.net/manual/en/function.json-encode.php
They have some examples that might assist with what you need, I think (have not used the json thing ...)

I am yes :/

PHP:
$quotes = array("USDCURZAR", "EURCURZAR", "GBPCURZAR");

foreach ($quotes as $quote) {
    $url = "http://api/$quote:currency";
    $json = file_get_contents($url);
    $json = json_decode($json);

    $currency_history = $json->priceTimeSeries->price;

    
    echo "<br>";
    echo $currency_history;

  
  
  
  
  
}

I suspect I will have to do a foreach loop inside the foreach loop O_O
/brain broken
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
you can just do the following in javascript

PHP:
let numberOfMilliseconds = Date.parse("2014-12-12 14:18:00");

This is the same as doing

PHP:
let numberOfMilliseconds = Date.UTC(2014, 12, 12, 14, 18);
 
Top