'Https://Pomber - Github.Io/Covid19/Timeseries - Json': Maxbox8 C:/Maxbox/Mx47464/Maxbox4/Docs/Maxbox - Starter82.Txt T: 5 P: 1
'Https://Pomber - Github.Io/Covid19/Timeseries - Json': Maxbox8 C:/Maxbox/Mx47464/Maxbox4/Docs/Maxbox - Starter82.Txt T: 5 P: 1
'Https://Pomber - Github.Io/Covid19/Timeseries - Json': Maxbox8 C:/Maxbox/Mx47464/Maxbox4/Docs/Maxbox - Starter82.Txt T: 5 P: 1
20 05/03/2021 17:58:32
1: //////////////////////////////////////////////////////
2: JSON
3: ______________________________________________________
4: maXbox Starter 82 -JSON in Code - Max Kleiner
5:
6: "There is always space for improvement"
7: — Oscar De La Hoya
8:
9:
10: JSON (JavaScript Object Notation) is a lightweight data-interchange
format. It is easy for humans to read and write. It is easy for machines
to parse and generate. A JSON Parser is then used to format the JSON data
into a properly readable JSON Format with curly brackets. That can easily
view and identify its key and values.
11:
12: {
13: "date": "2021-3-4",
14: "confirmed": 36223,
15: "deaths": 1483,
16: "recovered": 33632
17: }
18:
19: Reading JSON data in maXbox could be easy. Json data can be read from a
file or it could be a json web link. Let us first try to read the json
from a web link.
20:
21: Const
22: JsonUrl = 'https://pomber.github.io/covid19/timeseries.json';
23:
24: We use JSON for Delphi framework (json4delphi), it supports older versions
of Delphi and Lazarus (6 or above) and is very versatile. Another
advantage is the Object-pascal native code, using classes only TList,
TStrings, TStringStream, TCollection and TStringList; The package contains
3 units: Jsons.pas, JsonsUtilsEx.pas and a project Testunit, available at:
https://github.com/rilyu/json4delphi
25:
26: Now we need a Load URL or Upload File function to get the json data for
parsing. In our case load is a function-pair of open and send().
27:
28: Let us first define the necessary packages "msxml2.xmlhttp" and the JSON
class itself:
29:
30: var XMLhttp: OleVariant; // As Object Automation
31: ajt: TJson; JObj: TJsonObject2;
32:
33: XMLhttp:= CreateOleObject('msxml2.xmlhttp')
34: XMLhttp.Open('GET', JsonUrl, False) //False is async
35: XMLhttp.setRequestHeader('Content-Type','application/x-www-form-
urlencoded');
36: XMLhttp.Send();
37:
38: response:= XMLhttp.responseText; //assign the data
39: statuscode:= XMLhttp.status;
40:
41: Using async = false in Open() is not always recommended, but for a few
small requests this can be ok. Remember that the script will NOT continue
to execute, until the server response is ready. If the server is busy or
slow, the application will hang or stop.
42: Anyway we open our XMLhttpObject (which is late binding) as not
asynchronous, that is to say, synchronous because we need all data to
continue:
43:
MAXBOX8 C:\maXbox\mX47464\maxbox4\docs\maxbox_starter82.txt
http://www.softwareschule.ch/maxbox.htm T: 5
p: 1
maXbox4 4.7.5.20 05/03/2021 17:58:32
MAXBOX8 C:\maXbox\mX47464\maxbox4\docs\maxbox_starter82.txt
http://www.softwareschule.ch/maxbox.htm T: 5
p: 2
maXbox4 4.7.5.20 05/03/2021 17:58:32
93:
94: ...United Kingdom
95: Uruguay
96: Uzbekistan
97: Vanuatu
98: Venezuela
99: Vietnam...
100:
101: So the country is an object to get. Ok, it is a JsonObject dictionary with
192 countries. Lets check the keys of our dict with a nested loop of all
confirmed cases:
102:
103: for cnt:= 0 to Jobj.count-1 do begin
104: Clabel:= Jobj.items[cnt].name;
105: JArray2:= jobj.values[Clabel].asArray;
106: for cnt2:= 0 to jarray2.count-1 do
107: itmp:= jarray2.items[cnt2].asObject.values['confirmed'].asinteger;
108: end;
109:
110: So we pass the country name items[cnt].name from the Json Object in to the
value list and we get back an a object array for the second iteration of
at the moment 408 records with key values of integer format. Our dimension
for now is 192 * 408 = 78336 records.
111:
112: <class 'pandas.core.frame.DataFrame'>
113: RangeIndex: 78336 entries, 0 to 78335
114: Data columns (total 5 columns):
115: # Column Non-Null Count Dtype
116: --- ------ -------------- -----
117: 0 country 78336 non-null object
118: 1 date 78336 non-null object
119: 2 confirmed 78336 non-null int64
120: 3 deaths 78336 non-null int64
121: 4 recovered 78336 non-null int64
122: dtypes: int64(3), object(2)
123: memory usage: 3.0+ MB
124: {
125: "Afghanistan": [
126: {
127: "date": "2020-1-22",
128: "confirmed": 0,
129: "deaths": 0,
130: "recovered": 0
131: },
132: {
133: "date": "2020-1-23",
134: "confirmed": 0,
135: "deaths": 0,
136: "recovered": 0
137: },...
138:
139: In a second attempt we visualize the timeseries with TeeChart Standard. Ok
we got the objectarray as sort of dataframe with items and values but not
in the form that we wanted. We will have to unwind the nested data like
above to build a proper dataframe with chart series at runtime for TChart:
140:
141: Chart1.Title.Text.clear;
142: //AssignSeries(OldSeries,NewSeries:TChartSeries);
143: Chart1.Title.Text.add('Sciplot Serie: '+'World Covid21 confirmed not †');
MAXBOX8 C:\maXbox\mX47464\maxbox4\docs\maxbox_starter82.txt
http://www.softwareschule.ch/maxbox.htm T: 5
p: 3
maXbox4 4.7.5.20 05/03/2021 17:58:32
MAXBOX8 C:\maXbox\mX47464\maxbox4\docs\maxbox_starter82.txt
http://www.softwareschule.ch/maxbox.htm T: 5
p: 4
maXbox4 4.7.5.20 05/03/2021 17:58:32
MAXBOX8 C:\maXbox\mX47464\maxbox4\docs\maxbox_starter82.txt
http://www.softwareschule.ch/maxbox.htm T: 5
p: 5