WEBVTT

00:00:00.700 --> 00:00:06.780
This lesson converts a small CSV file into a JSON array of active users.

00:00:06.930 --> 00:00:14.405
We will preserve a name containing a comma, convert ages into numbers, and turn active flags into

00:00:14.405 --> 00:00:14.845
Booleans.

00:00:14.995 --> 00:00:18.771
Those details make this more than a file extension change.

00:00:18.921 --> 00:00:25.385
Open fixtures slash input dot CSV, then run the supplied converter from examples.

00:00:25.535 --> 00:00:31.188
The generated JSON and CSV files will appear inside the out directory.

00:00:33.340 --> 00:00:36.988
CSV allows quoted fields containing commas.

00:00:37.138 --> 00:00:43.943
Splitting every input line on a comma would damage Sam Junior's name by treating it as two fields.

00:00:44.093 --> 00:00:48.424
Use parse_csv to interpret records according to the format.

00:00:48.574 --> 00:00:52.734
With header set to true, the first record supplies field names.

00:00:52.884 --> 00:01:01.079
The parser returns a stream of Maps, so subsequent callbacks can access row dot name, row dot age, and

00:01:01.079 --> 00:01:02.804
row dot active explicitly.

00:01:04.950 --> 00:01:07.851
CSV does not infer a schema for us.

00:01:08.001 --> 00:01:13.249
The active field is a String containing the letters true, not a Boolean value yet.

00:01:13.399 --> 00:01:16.770
Our filter therefore compares it with a quoted string.

00:01:16.920 --> 00:01:22.893
This is an easy place to make a silent logic mistake by comparing unlike types.

00:01:23.043 --> 00:01:29.549
Confirm the representation at the input boundary, and decide whether filtering should happen before

00:01:29.549 --> 00:01:33.731
conversion or after a separate validation and conversion stage.

00:01:35.880 --> 00:01:41.021
The projection constructs a fresh Map with exactly the fields we want to publish.

00:01:41.171 --> 00:01:47.401
To_int converts each retained age, and the equality expression produces a real Boolean.

00:01:47.551 --> 00:01:52.713
Collect materializes the stream as a List, which becomes a JSON array.

00:01:52.863 --> 00:01:57.130
This small lesson uses tiny fixtures, so that is appropriate.

00:01:57.280 --> 00:02:04.510
For a very large dataset, think about memory limits and whether your destination can accept records

00:02:04.510 --> 00:02:07.221
incrementally instead of one complete array.

00:02:09.370 --> 00:02:15.002
Encode_json turns ordinary HHY values into JSON text.

00:02:15.152 --> 00:02:19.888
Pretty mode adds readable formatting without changing the data types.

00:02:20.038 --> 00:02:23.110
Save_text writes that one complete String to disk.

00:02:23.260 --> 00:02:29.831
Do not send a lazy Stream or a filesystem object directly to the JSON encoder.

00:02:29.981 --> 00:02:33.671
First consume or project it into supported values.

00:02:33.821 --> 00:02:41.181
Our output is a List of ordinary Maps, and each Map contains only a String, an integer, and a Boolean.

00:02:43.330 --> 00:02:46.871
A round trip is a practical verification step.

00:02:47.021 --> 00:02:51.267
Read the file, parse it back, and inspect important properties.

00:02:51.417 --> 00:02:53.486
There should be two retained users.

00:02:53.636 --> 00:02:59.865
The first name is Ada, the age reports Int, and the second name still includes its comma.

00:03:00.015 --> 00:03:05.349
This checks filtering, conversion, encoding, and quoted CSV handling together.

00:03:05.499 --> 00:03:11.323
Merely seeing that a file exists would not prove any of those requirements were met correctly.

00:03:13.470 --> 00:03:18.803
To encode CSV, convert our collected List back into a Stream explicitly.

00:03:18.953 --> 00:03:26.761
Encode_csv returns text records without line terminators, and save_lines supplies those terminators.

00:03:26.911 --> 00:03:33.781
Reusing the same users List is safe because we are constructing a fresh stream for this operation.

00:03:33.931 --> 00:03:40.032
Do not assume the generated CSV will preserve every formatting choice from the input.

00:03:40.182 --> 00:03:46.006
The purpose is to preserve selected data, with serialization handled by the encoder.

00:03:48.160 --> 00:03:53.131
Malformed JSON should not quietly become a successful empty result.

00:03:53.281 --> 00:03:58.742
Here we deliberately pass invalid text and show a clear failure marker from catch.

00:03:58.892 --> 00:04:06.526
In a production converter, decide whether an invalid record should stop the job or become an explicit

00:04:06.526 --> 00:04:07.873
rejected record report.

00:04:08.023 --> 00:04:12.738
Never substitute a plausible default merely to make the script finish.

00:04:12.888 --> 00:04:19.480
The next lesson shows how modules and per operation Results help express those decisions cleanly.

00:04:21.630 --> 00:04:29.041
For practice, add another active user whose quoted name contains a comma, then check the output count and

00:04:29.041 --> 00:04:29.865
preserved name.

00:04:30.015 --> 00:04:36.009
Next, in a copy of the fixture, replace an active user's age with nonnumeric text.

00:04:36.159 --> 00:04:40.533
Observe the conversion failure and consider where you would validate it.

00:04:40.683 --> 00:04:44.864
Keep the original fixture intact for repeatable comparison.

00:04:45.014 --> 00:04:53.335
You can now move between CSV records and JSON values while controlling selection, field types, and output

00:04:53.335 --> 00:04:53.825
structure.

