From 097eaa0500dae283ae580dee2ea805f0abbbdd4a Mon Sep 17 00:00:00 2001 From: Ludwig Behm Date: Wed, 18 Oct 2023 17:26:25 +0200 Subject: [PATCH] Fix: removed usage pick(); Refactored map() usage; moved `type` property into prefix addition --- README.md | 6 +++--- jcal-unfuck.jq | 17 ++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2eb70c8..63f6199 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ This oneliner converts streaming optimised jCal format from [rfc7265](https://da ## The actual code -`jq '[(.[2][]|select(.[0] == "vevent")|.[1]|[.[]|{"key": .[0], "value": .[3]}]|from_entries)]'` +`jq '.[2] | map({_type: .[0]} + (.[1] | map({key: .[0], value: .[3]}) | from_entries))'` ## Example Usage ```bash -curl -sH "Accept: application/calendar+json" https://cloud.kraut.space/remote.php/dav/public-calendars/2EkPGt3PF6WwYsA3\?export\&expand\=1\&start\=$(date -d '' +%s)\&end\=$(date -d 'next month' +%s) \ -| jq '[(.[2][]|select(.[0] == "vevent")|.[1]|[.[]|{"key": .[0], "value": .[3]}]|from_entries)]' +curl -sH 'Accept: application/calendar+json' https://cloud.kraut.space/remote.php/dav/public-calendars/2EkPGt3PF6WwYsA3\?export\&expand\=1\&start\=$(date -d '' +%s)\&end\=$(date -d 'next month' +%s) \ +| jq '.[2]|map({_type:.[0]}+(.[1]|map({key:.[0],value:.[3]})|from_entries))' ``` diff --git a/jcal-unfuck.jq b/jcal-unfuck.jq index 401435c..76cea29 100755 --- a/jcal-unfuck.jq +++ b/jcal-unfuck.jq @@ -1,10 +1,13 @@ #!/usr/bin/jq -rfc -[ - ( - .[2][] # skipping calender header and accessing events - | [["_type", {}, "text", .[0]], .[1][]] # adding ical type like 'vevent' as ._type property - | [.[] | {"key": .[0], "value": .[3]}] | from_entries # transforming event array structure into object structure - | pick(._type, .dtstart, .dtend, .summary, .location, .description, .sequence, .["related-to"]) # allow list of properties + +.[2] # skipping calender header and accessing events +| map( # for each event: + {_type: .[0]} # adding ical type like 'vevent' as ._type property + + ( + .[1] # accessing jCal properties; [1] holds name of iCalendar component, i.e. `vevent`; [2] holds additional components + | map({key: .[0], value: .[3]}) | from_entries # transforming event array structure into object structure + # only tested with vevents. pick() isn't supported in jq on debian bookworm + # | pick(.dtstart, .dtend, .summary, .location, .description, .sequence, .["related-to"]) # allow list of properties ) -] +) | sort_by(.dtstart|fromdate) # sorting by event start time