16. Using JSON strings
15. Displaying data in a dynamic number of column groups
stations.csv and station2.json store latitudes, longitudes and elevation data of certain observation stations. Get information of stations whose names begin with letter B, sort records by station name, and return result as JSON strings.
Expected result:
SPL code:
A | |
---|---|
1 | =T(“stations.csv”) |
2 | =json(file(“stations2.json”).read()) |
3 | =(A1|A2).select((first=left(STATION,1), first==“B” || first==“C”)).sort(STATION) |
4 | =json(A3) |
json() funciton is conveninet for handling computing tasks involving JSON strings. It automatically identifies the parameter type to transform a table sequence or record to JSON strings, or reads JSON strings as a table sequence. A1 reads data from the csv file as a table sequence:
A2 reads strings from the JSON file and transform it a table sequence:
A2’s code of reading data from the JSON file as a table sequence can be also changed to =T(“stations2.json”). T() function can automatically identify the file type according to the extension. A3 concatenates data of both table sequences using A|B, which puts A2’s records after A1’s, to perform the query. The concatenation does not require that data should come from the same table and even that they have completely same fields. But they must contain fields involved in the subsequent operations, such as the select operation. Here is A3’s result set:
A4 continues to use json()function to convert the result table sequence to JSON strings. If needed, use f.write(T) function to write data of the table sequence to a JSON file.
17. Large join query
Contents and Exercise Data
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/2bkGwqTj
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version