Transform the JSON into a Table for easier manipulation | Robocorp documentation (2024)

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:

from robocorp.tasks import taskfrom RPA.HTTP import HTTPfrom RPA.JSON import JSONfrom RPA.Tables import Tableshttp = HTTP()json = JSON()table = Tables()@taskdef produce_traffic_data(): """ Inhuman Insurance, Inc. Artificial Intelligence System automation. Produces traffic data work items. """ http.download( url="https://github.com/robocorp/inhuman-insurance-inc/raw/main/RS_198.json", target_file="output/traffic.json", overwrite=True, ) traffic_data = load_traffic_data_as_table()@taskdef consume_traffic_data(): """ Inhuman Insurance, Inc. Artificial Intelligence System robot. Consumes traffic data work items. """ print("consume")def load_traffic_data_as_table(): json_data = json.load_json_from_file("output/traffic.json") table_from_json = table.create_table(json_data["value"]) return table_from_json

You add a new load_traffic_data_as_table() function that returns the JSON data in a Table format.

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:

table.write_table_to_csv(traffic_data, "output/test.csv")

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!

Transform the JSON into a Table for easier manipulation | Robocorp documentation (1)

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!

from robocorp.tasks import taskfrom RPA.HTTP import HTTPfrom RPA.JSON import JSONfrom RPA.Tables import Tableshttp = HTTP()json = JSON()table = Tables()TRAFFIC_JSON_FILE_PATH = "output/traffic.json"@taskdef produce_traffic_data(): """ Inhuman Insurance, Inc. Artificial Intelligence System automation. Produces traffic data work items. """ http.download( url="https://github.com/robocorp/inhuman-insurance-inc/raw/main/RS_198.json", target_file=TRAFFIC_JSON_FILE_PATH, overwrite=True, ) traffic_data = load_traffic_data_as_table() table.write_table_to_csv(traffic_data, "output/test.csv")@taskdef consume_traffic_data(): """ Inhuman Insurance, Inc. Artificial Intelligence System robot. Consumes traffic data work items. """ print("consume")def load_traffic_data_as_table(): json_data = json.load_json_from_file(TRAFFIC_JSON_FILE_PATH) return table.create_table(json_data["value"])

Transform the JSON into a Table for easier manipulation | Robocorp documentation (2024)

FAQs

How to convert JSON format into table? ›

You add a new load_traffic_data_as_table() function that returns the JSON data in a Table format.
  1. The load_json_from_file() function provided by the RPA. JSON library returns the file contents (string) in JSON format.
  2. The create_table() function from the RPA. Tables library converts the JSON format into a Table.

How to transform JSON to table in Excel? ›

6 Steps: Import JSON In Excel
  1. Open MS Excel. Open the Start Menu using Windows Keys or Clicking the Start Menu icon on your Task Bar. ...
  2. Create New Workbook. Next, you need to create a blank spreadsheet. ...
  3. Locate the Data Tab. ...
  4. Import Data. ...
  5. Select your JSON File. ...
  6. Convert Data Into Table.

How to create a table from a JSON file? ›

Loop through the JSON data, create table rows, get the values of the current object in the JSON data using Object. values(item), and create table cells. Set the value as the text of the table cell, append the table cells to the table row, and then append the table row to the table.

How to manipulate data in JSON file? ›

Manipulating JSON data involves modifying or adding properties to JavaScript objects obtained from parsing JSON. To modify a property, you can directly assign a new value to it using the dot notation or square bracket notation. To add a property, you can use the same notation and assign a value to a new property name.

How to convert JSON data into table format online? ›

How to use a json2table converter. All you need to do is to paste your JSON into the input field above and, and hit the button 'Convert to table'. To be able to fit a two-dimensional table, up to 5 levels are flattened so that each attribute finds its place in different columns and each object lines up in a row.

How to convert JSON into SQL table? ›

Here's how to convert a JSON file into a SQL database table insert statement, for free, using SQLizer:
  1. Visit SQLizer Website: Go to the SQLizer website.
  2. Upload JSON File: Click on the “Browse” button to upload your JSON file. ...
  3. Select Output Format: Choose “SQL” as the output format.
Oct 25, 2023

How to convert a table into JSON format? ›

Use select transformation to unselect the source columns. Use Sink transformation to load the transformed data into JSON file. In the sink settings, provide file name option as 'Output to single file' and provide the filename . Also, set single partition in optimize tab.

How to convert JSON file from Excel? ›

Excel to JSON using VBA code editor

Besides using all these tools, you can use the in-built feature of Excel (VBA code editor) to convert the Excel data to JSON format. Make a code for it and execute the code; it will do the mapping of Excel columns to JSON object keys and convert the data into JSON.

How to convert JSON array into table? ›

How to Convert JSON Array to HTML Table Online Online?
  1. Upload or paste your JSON Array. Before using the JSON converter, please make sure that your JSON is in the format of an array of objects. ...
  2. Edit your JSON Array online if needed. ...
  3. Copy the converted HTML Table.

How to Convert JSON data into table format in Oracle? ›

Convert Oracle JSON data available in tables. The SQL/JSON function JSON_TABLE creates a relational view of JSON data. It maps the result of a JSON data evaluation into relational rows and columns. You can query the result returned by the function as a virtual relational table using SQL.

How to create a database from JSON? ›

When executed, the project will read the data from the JSON file and load that data into RowSet variables using the Read JSON task. Those RowSets are then used to insert the data into two separate database tables using the SQL task.

How to extract data from JSON format? ›

To extract JSON data from a column and put it in separate columns:
  1. Go to the column in the workbook. ...
  2. Use the column's menu to select Extract columns. ...
  3. In the modal, select the fields you want to pull out into their own columns.
  4. Click Confirm. ...
  5. Use the new columns in your explorations and analysis.

What is the tool for manipulating JSON? ›

JSONtoolkit is a multi-activity tool for manipulating JSON. Allows editing of JSON properties, adding or removing properties; query using LINQ and TokenSelect to filter JSON. It also allows conversions between JSON and XML.

What does JSON stand for? ›

JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

What is the best way to modify JSON? ›

You can use any text/code editor such as Visual Studio Code, Notepad, Notepad++, Sublime Text and others to open and edit JSON files. Alternatively, you can use an online editor to edit your JSON files.

How to view JSON file in table format? ›

The JSON data is converted to table format using following rules: If JSON data is directly an array, then each item of that array is shown as a row in the table. If JSON data is an object, then the items with in the first array field are shown as table rows.

How to convert the JSON format? ›

Stringify a JavaScript Object

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How to convert JSON data into table format in MySQL? ›

One such way of importing a JSON document into MySQL is by converting JSON to tabular data with the JSON_TABLE(expression, path) function. The first argument, expression , returns JSON data, while the second argument, path , specifies a JSON path that is applied to the JSON document during conversion.

Top Articles
Coverage D & Additional Living Expenses | Goosehead Insurance
Costa Rica Travel Insurance Requirements
Maxtrack Live
Uca Cheerleading Nationals 2023
Craigslist Monterrey Ca
Ups Dropoff Location Near Me
Part time Jobs in El Paso; Texas that pay $15, $25, $30, $40, $50, $60 an hour online
Algebra Calculator Mathway
Vaya Timeclock
Zitobox 5000 Free Coins 2023
30% OFF Jellycat Promo Code - September 2024 (*NEW*)
Calamity Hallowed Ore
Bill Devane Obituary
Infinite Campus Parent Portal Hall County
Clairememory Scam
Love Compatibility Test / Calculator by Horoscope | MyAstrology
Lesson 2 Homework 4.1
4156303136
Jvid Rina Sauce
Payment and Ticket Options | Greyhound
Jenn Pellegrino Photos
Nhl Wikia
Lonesome Valley Barber
Jalapeno Grill Ponca City Menu
Jbf Wichita Falls
Schedule 360 Albertsons
The Weather Channel Local Weather Forecast
1973 Coupe Comparo: HQ GTS 350 + XA Falcon GT + VH Charger E55 + Leyland Force 7V
Village
Regal Amc Near Me
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
4 Times Rihanna Showed Solidarity for Social Movements Around the World
Geico Car Insurance Review 2024
Yayo - RimWorld Wiki
Craigslist Middletown Ohio
Tokioof
Max 80 Orl
P3P Orthrus With Dodge Slash
Audi Q3 | 2023 - 2024 | De Waal Autogroep
Space Marine 2 Error Code 4: Connection Lost [Solved]
Craigslist Boats Eugene Oregon
Invalleerkracht [Gratis] voorbeelden van sollicitatiebrieven & expert tips
Giovanna Ewbank Nua
Mauston O'reilly's
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
The Pretty Kitty Tanglewood
Jimmy John's Near Me Open
Is My Sister Toxic Quiz
Lux Funeral New Braunfels
Rétrospective 2023 : une année culturelle de renaissances et de mutations
Kenmore Coldspot Model 106 Light Bulb Replacement
Cognitive Function Test Potomac Falls
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5994

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.