Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Using GPT-3 to figure out jq recipes#
I like jq, but I always have to think pretty hard about how to construct the right syntax for it.
Here’s how I used the GPT-3 playground to solve a simple jq problem.
I wanted to sum up the total number of comments in JSON that looks like this (truncated example):
1[2 {3 "id": "31195431",4 "title": "Automatically filing issues when tracked file content changes",5 "url": "https://simonwillison.net/2022/Apr/28/issue-on-changes/",6 "dt": "2022-04-28T17:29:37",7 "points": 14,8 "submitter": "simonw",9 "commentsUrl": "https://news.ycombinator.com/item?id=31195431",10 "numComments": 411 },12 {13 "id": "31185496",14 "title": "Parallel SQL Queries in Datasette",15 "url": "https://simonwillison.net/2022/Apr/27/parallel-queries/",16 "dt": "2022-04-27T20:39:50",17 "points": 1,18 "submitter": "frogger8",19 "commentsUrl": "https://news.ycombinator.com/item?id=31185496",20 "numComments": 321 }22]The first prompt I tried was:
1# Use jq to calculate the sum of the num field in each object in this array:2
3echo '[4 {"num": 1},5 {"num": 2}6]' | jqI hinted at how I wanted to do this by piping to jq but without any argument.
GPT-3 completed that to:
1echo '[2 {"num": 1},3 {"num": 2}4]' | jq 'reduce .[] as $item (0; . + $item.num)'… then a bunch more output that gave further examples, which I ignored
This is good - that solution works! Here’s what happens when I run it:
1~ % echo '[2 {"num": 1},3 {"num": 2}4]' | jq 'reduce .[] as $item (0; . + $item.num)'53But I’m not a big fan of reduce in general - I always have to think a bit about what it’s actually doing.
So I added a follow-up prompt:
1Now do that without using reduce:And GPT-3 replied:
1echo '[2 {"num": 1},3 {"num": 2}4]' | jq '. | map(.num) | add'This is the exact solution I was looking for.