I'm trying to convert this CSV
dir1/key1=val1/,MOVED_TO,123.pq
dir2/key1=val2/key2=val3/,MOVED_TO,456.pq
into this desired output (line-oriented json):
{"type":"quux","top"="dir1","key1":"val1","event":"MOVED_TO"}
{"type":"quux","top"="dir2","key1":"val2","key2":"val3","event":"MOVED_TO"}
So far I've gotten close, but I still have $key nested.
echo -e 'dir1/key1=val1/,MOVED_TO,123.pq
dir2/key1=val2/key2=val3/,MOVED_TO,456.pq' | \
mlr --hi --records-per-batch 1 --csv --allow-ragged-csv-input --hi --no-jlistwrap --ojsonl \
label dirname,event,filename \
then put '$type = "quux"; $top = sub($dirname,"/.*",""); $key = splitkv(sub(sub($dirname, "^[^/]*/", ""), "/$", ""), "=", "/")' \
then cut -f type,top,key,event
{"event": "MOVED_TO", "type": "quux", "top": "dir1", "key": {"key1": "val1"}}
{"event": "MOVED_TO", "type": "quux", "top": "dir2", "key": {"key1": "val2", "key2": "val3"}}
I've been trying to use concat(.) but that seems to work on arrays and not maps as I have there.
The "type":"quux" is a static string I need to assign during the process, it is not present in the raw CSV.
How can I unnest $key?
"quux"will change based on location. It is not in the raw data, ergo why I'm adding it as a string literal in themlrcode.