Transform the JSON into a Table for easier manipulation
You need to do quite a bit of filtering and transformation to get the desired result. As you have learned, a good strategy for tackling larger problems is to solve the problem in small steps.
You decide to scope the first step so that the result is the raw JSON data transformed into a Table (a data structure provided by the RPA.Tables
library) that is then easy to filter, sort, group, etc.:
producer.robot
:
You add a new load_traffic_data_as_table()
function that returns the JSON data in a Table format.
- The
load_json_from_file()
function provided by theRPA.JSON
library returns the file contents (string) in JSON format. - The
create_table()
function from theRPA.Tables
library converts the JSON format into a Table.
You run the robot and view the log. Your robot has transformed the raw JSON data into a Table with lots of rows:
Table( columns=[ 'Id', 'IndicatorCode', 'SpatialDimType', 'SpatialDim', 'TimeDimType', 'TimeDim', 'Dim1Type', 'Dim1', 'Dim2Type', 'Dim2', 'Dim3Type', 'Dim3', 'DataSourceDimType', 'DataSourceDim', 'Value', 'NumericValue', 'Low', 'High', 'Comments', 'Date', 'TimeDimensionValue', 'TimeDimensionBegin', 'TimeDimensionEnd' ], rows=11640 )
Viewing the whole table using a VS Code extensions
The table entry in the log is nice and concise but lacks the actual data.
To view the entire table, you decide to export the Table into a CSV file and view that as a human-readable representation using the Excel Viewer VS Code extension.
You call the super-handy Write table to CSV keyword:
After rerunning the robot, you see a test.csv
file at the root. Clicking on it opens the raw CSV file. You click on the Open Preview
icon at the top-right. A preview of the data is displayed as a table. What a nice way to manually inspect the intermediate results!
DRY - Don't repeat yourself
Always in search for tidy code, you see that you have broken the Don't repeat yourself principle by duplicating the path to the traffic.json
file.
It's not a massive deal at the moment since you have so little code, but it still requires future maintainers to find and change the path in two places if the path needs to be changed.
Little repetition might eventually grow into lots of small repetition and make it slower to maintain your robot or even cause subtle bugs if someone forgets to replace all the instances of the duplicated things.
You decide to store the path of the traffic.json
file into a variable and then refer to that variable instead. This way, you remove the duplication:
You also notice, that in load_traffic_data_as_table()
you define a variable just to return it.
But, you read that you can return an expression to be evaluated, not just a variable.
So we are going to refactor this as well!