Challenge #5: Flattening JSON

For this challenge, we ask the https://randomuser.me/ API to generate random user data for us. The request returns 10 user accounts in a JSON response.

We need to transform the JSON response into one row per user and extract some fields: first_name, last_name, gender, age, nationality, country, location_latitude, location_longitude and the cell phone number.

The data should looks like this:

Use start.dfl (6.1 KB) to get started.

The solution for this challenge involves parsing the JSON string, iterating over the array of user accounts, and extracting info from the nested data structures.

Download the solution.dfl (20.3 KB) file to follow along.

Step 1: Parsing

The HTTP step is a good place to parse the HTTP repsonse string to JSON, and placing it into the row stream. The JSON object becomes a corresponding tweakflow data value.

Step 2: Iterating over accounts

The Get Items step emits each item contained in json[:results] into the row stream, giving us individual user dicts to work on.

Step 3: Extracting user data

Now that we have individual users to work on, we can extract user data by accessing the corresponding keys. Note that foo[:key_1, :key_2] is a shorthand for foo[:key_1][:key_2],allowing slightly more compact notation for consecutive key access.

Step 4: Cleanup

The solution uses the Pick Fields step to remove the json and user objects from the row stream.

The final result

The row stream contains the data as required: