Skip to content

Instantly share code, notes, and snippets.

@jimsander
Last active April 15, 2022 22:22
Show Gist options
  • Save jimsander/eb952d052cabf36cd437ac7ea1915c6d to your computer and use it in GitHub Desktop.
Save jimsander/eb952d052cabf36cd437ac7ea1915c6d to your computer and use it in GitHub Desktop.
Sample JQ parsing
# Simple CSV to JSON converter.
# No files, just stdin and stdout like a real tool should be
# Jim Sander
import csv
import json
import sys
data = []
with sys.stdin as csvf:
csvReader = csv.DictReader(csvf)
for row in csvReader:
data.append(row)
sys.stdout.write(json.dumps(data, indent=4))

Converting Asana to TaskWarrior tasks

  1. Export Asana tasks as csv
  2. convert to json (see cjParse.py
  3. Run through jq
  4. Run through sed and awk $ cat ~/Documents/BrainDump.json | jq -r '.[1,2]' { "Task ID": "38330760681093", "Created At": "2015-06-18", "Completed At": "", "Last Modified": "2022-04-15", "Name": "Sensu Client : remote server configuration setup", "Section/Column": "(no section)", "Assignee": "Jim Sander", "Assignee Email": "[email protected]", "Start Date": "", "Due Date": "", "Tags": "", "Notes": "", "Projects": "BrainDump,DCM-Sensu" } { "Task ID": "38874426003420", "Created At": "2015-06-23", "Completed At": "", "Last Modified": "2022-04-15", "Name": "Remote Monitor", "Section/Column": "(no section)", "Assignee": "Jim Sander", "Assignee Email": "[email protected]", "Start Date": "", "Due Date": "", "Tags": "", "Notes": "", "Projects": "BrainDump,DCM-Sensu" }

$ cat ~/Documents/BrainDump.json | jq -r '.[]|.Name,.Projects' | head -4 Sensu Check: Client Keepalive BrainDump,DCM-Sensu Sensu Client : remote server configuration setup BrainDump,DCM-Sensu

$ cat ~/Documents/BrainDump.json | jq '.[]| @sh "(.Name) (.Projects)"' | head -2 "'Sensu Check: Client Keepalive' 'BrainDump,DCM-Sensu'" "'Sensu Client : remote server configuration setup' 'BrainDump,DCM-Sensu'"

Now to parse it into 'taskwarrior' commands

cat ~/Documents/BrainDump.json | jq  '.[]| @sh "\(.Name)___\(.Projects)"' | sed "s/^\"'//" | sed "s/'\"$//" | sed "s/'___'/___/" | sed "s/ ___/___/" | awk -F'___' '{ pro=""; split($2,p,","); for (i in p){ pro=sprintf("pro:%s %s",p[i],pro)  }; 
printf("task add %s %s\n", $1, pro ) }' | head -3

task add Sensu Check: Client Keepalive pro:BrainDump pro:DCM-Sensu task add Sensu Client : remote server configuration setup pro:BrainDump pro:DCM-Sensu task add Remote Monitor pro:BrainDump pro:DCM-Sensu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment