Skip to main content

Generate and execute jq programs using LLMs

Project description

jqai

Always forgetting the jq syntax? Wish you had a magic box that translated your desires to the right syntax? Look no further than jqai!

Generate and execute jq programs using LLMs. Transform JSON data using natural language descriptions instead of learning complex jq syntax.

Usage

cat data.json | uvx jqai "get all user names and emails"

Prerequisites

  • jq: Must be installed on your system. Install jq
  • API Key: OpenAI API key or Echo API key

Setting up API Key

Option 1: OpenAI API Key

export OPENAI_API_KEY="your-openai-api-key"

Option 2: Echo API (Recommended)

Get an Echo API key at echo.merit.systems:

export OPENAI_API_KEY="echo_your-echo-api-key"

Usage

jqai works in two modes:

1. Piped JSON Mode

Transform JSON data by piping it to jqai:

cat data.json | jqai "get all user names and emails"
curl -s https://api.github.com/users/octocat | jqai "extract login, name, and public repo count"

2. Command Generation Mode

Generate complete commands that fetch and process data:

jqai "show the latest 5 issues from simonw/datasette repo"
jqai "get weather data for San Francisco and extract temperature"

Examples

Basic JSON Transformation

Given a JSON file users.json:

{
  "users": [
    {"name": "Alice", "age": 30, "city": "New York"},
    {"name": "Bob", "age": 25, "city": "San Francisco"},
    {"name": "Charlie", "age": 35, "city": "New York"}
  ]
}

Extract names and cities:

cat users.json | jqai "get names and cities of all users"

Filter users from New York:

cat users.json | jqai "show only users from New York"

Count users by city:

cat users.json | jqai "count users by city"

API Data Processing

Process GitHub API data:

curl -s https://api.github.com/repos/microsoft/vscode/issues | jqai "show issue titles and their authors"

Get repository statistics:

curl -s https://api.github.com/repos/python/cpython | jqai "extract name, stars, forks, and primary language"

Complex Transformations

Group and aggregate data:

cat sales.json | jqai "group sales by product and sum the amounts"

Nested object manipulation:

cat complex.json | jqai "flatten the nested user profiles and extract email domains"

Command Line Options

Option Short Description Default
--model -m OpenAI model to use gpt-4.1-mini
--preview-length -p Bytes of input for LLM context 1024
--output-only -o Show generated program only False
--silent -s Reduce output verbosity True
--verbose -v Show prompts and responses False

Examples with Options

Generate program without executing:

cat data.json | jqai "extract user emails" --output-only

Use a different model:

cat data.json | jqai "complex analysis" --model gpt-4-turbo-preview

Verbose mode for debugging:

cat data.json | jqai "transform data" --verbose

Advanced Usage

Learning jq

Use --output-only to see the generated jq programs and learn:

$ cat users.json | jqai "get names of users over 30" --output-only
.users[] | select(.age > 30) | .name

Custom Preview Length

For large JSON files, adjust the preview length:

cat large-file.json | jqai "analyze data structure" --preview-length 2048

Combining with Other Tools

Chain with other command-line tools:

jqai "get latest commits from torvalds/linux" | head -10
cat logs.json | jqai "extract error messages" | grep -i "critical"

How It Works

  1. Input Analysis: jqai analyzes your natural language description
  2. Context Building: For piped data, it reads a preview to understand the JSON structure
  3. Program Generation: Uses LLM to generate an appropriate jq program or shell command
  4. Execution: Runs the generated program with your data

Piped Mode Flow

JSON Data → Preview → LLM → jq Program → Execute → Output

Command Mode Flow

Description → LLM → Shell Command → Execute → Output

Error Handling

jqai handles common errors gracefully:

  • Missing API Key: Clear instructions on how to set up authentication
  • jq Errors: Passes through jq error messages for debugging
  • Network Issues: Proper error reporting for API calls
  • Broken Pipes: Handles interrupted output streams

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Troubleshooting

Common Issues

"jq: command not found"

  • Install jq: brew install jq (macOS) or apt-get install jq (Ubuntu)

"OPENAI_API_KEY environment variable is not set"

  • Set your API key: export OPENAI_API_KEY="your-key"
  • For Echo API, get a key at echo.merit.systems

Generated program doesn't work as expected

  • Use --verbose to see the generated program
  • Try --output-only to inspect the jq syntax
  • Provide more specific descriptions in your natural language query

Getting Help

For more complex queries, try being more specific:

Instead of: "process the data" Try: "extract user names and count them by department"

Instead of: "fix the JSON" Try: "remove null values and flatten nested objects"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jqai-0.1.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jqai-0.1.4-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file jqai-0.1.4.tar.gz.

File metadata

  • Download URL: jqai-0.1.4.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.21

File hashes

Hashes for jqai-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d1950745a380df99615813b6ebf673ee08652396ef53d42acbe683c9cb562421
MD5 4ab217264e8c2b123385d5785594deae
BLAKE2b-256 d10df528a48dd3dc6acd62c0332eeb6eac585e8d84ddc5554f298980b746e27f

See more details on using hashes here.

File details

Details for the file jqai-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: jqai-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.21

File hashes

Hashes for jqai-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b3520acb0e44de05cefe5a7643c4ae7b18920b1e1bfcfcc108e3d50dc7e4b559
MD5 e8d632e773790ca8be664a74095a1c14
BLAKE2b-256 13a1cf0280077e16e2fe89cf2a78c520aaad9160866ec420510b70869be15e48

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page