Merging nested data
The HR team stores employee data across two separate JSON files: one containing basic information and another containing additional attributes. To build a complete employee profile, you need to join these tables using a common identifier.
An inner join combines rows from two tables where the join column matches - like merging two guest lists by name. The JsonReader, JsonReadOptions, and Table classes have been imported for you.
This exercise is part of the course
Importing Data in Java
Exercise instructions
- Set the table name for the first JSON file.
- Complete the builder chain for the second file.
- Join the tables on the
"id"column.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class NestedEmployees {
public static void main(String[] args) {
// Set table name for first JSON
JsonReadOptions opts1 = JsonReadOptions
.builder("employees.json")
.____("Main")
.build();
Table main = new JsonReader().read(opts1);
// Complete builder chain for second JSON
JsonReadOptions opts2 = JsonReadOptions
.builder("employees_extra.json")
.tableName("Extra")
.____();
Table extra = new JsonReader().read(opts2);
System.out.println("Main columns: " + main.columnNames());
System.out.println("Extra columns: " + extra.columnNames());
// Join tables on id column
Table joined = main.____("id").____(extra);
System.out.println("\nJoined table (inner join on id):");
System.out.println(joined.print());
}
}