Question Details

No question body available.

Tags

json miller

Answers (1)

Accepted Answer Available
Accepted Answer
November 3, 2025 Score: 1 Rep: 5,907 Quality: High Completeness: 80%

I managed to solve this! The key was using Miller's nest --explode with the right combination of separators:

mlr --icsv --hi --ojsonl --from input.csv \
  cat \
  then put '$top=regextract($1,"dir\d+");$1=sub($1,"dir\d+/","");$1=sub($1,"/$","")' \
  then cat -n \
  then rename 1,keys,2,event \
  then nest --explode --pairs --across-fields --nested-fs "/" --nested-ps "=" -f keys \
  then cut -x -f 3

Output:

{"n": 1, "key1": "val1", "event": "MOVEDTO", "top": "dir1"}
{"n": 2, "key1": "val2", "key2": "val3", "event": "MOVEDTO", "top": "dir2"}

The key steps are:

  1. Extract the top directory prefix using regextract()

  2. Clean up the first field by removing the prefix and trailing slash

  3. Add a record number with cat -n

  4. Rename fields to prepare for nesting

  5. Use nest --explode --pairs --across-fields with --nested-fs "/" (field separator) and --nested-ps "=" (pair separator) to properly split and unnest the key-value pairs

  6. Remove the 3 column with cut -x -f 3

The crucial part was using --across-fields instead of --across-records, and specifying both --nested-fs for splitting the pairs and --nested-ps for splitting each key from its value. This properly flattens the nested structure into the parent record.