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):

[
    {
        "id": "31195431",
        "title": "Automatically filing issues when tracked file content changes",
        "url": "https://simonwillison.net/2022/Apr/28/issue-on-changes/",
        "dt": "2022-04-28T17:29:37",
        "points": 14,
        "submitter": "simonw",
        "commentsUrl": "https://news.ycombinator.com/item?id=31195431",
        "numComments": 4
    },
    {
        "id": "31185496",
        "title": "Parallel SQL Queries in Datasette",
        "url": "https://simonwillison.net/2022/Apr/27/parallel-queries/",
        "dt": "2022-04-27T20:39:50",
        "points": 1,
        "submitter": "frogger8",
        "commentsUrl": "https://news.ycombinator.com/item?id=31185496",
        "numComments": 3
    }
]

The first prompt I tried was:

# Use jq to calculate the sum of the num field in each object in this array:

echo '[
  {"num": 1},
  {"num": 2}
]' | jq

I hinted at how I wanted to do this by piping to jq but without any argument.

GPT-3 completed that to:

echo '[
  {"num": 1},
  {"num": 2}
]' | 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:

~ % echo '[
  {"num": 1},
  {"num": 2}
]' | jq 'reduce .[] as $item (0; . + $item.num)'
3

But 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:

Now do that without using reduce:

And GPT-3 replied:

echo '[
  {"num": 1},
  {"num": 2}
]' | jq  '. | map(.num) | add'

This is the exact solution I was looking for.

Created 2022-08-10T08:23:36-07:00 · Edit