Examples

Simple JSON

Consider simple.json:

[
  {"id": 1, "name": "Alice", "age": 30, "city": "New York"},
  {"id": 2, "name": "Bob", "age": 25, "city": "Los Angeles"},
  {"id": 3, "name": "Charlie", "age": 35, "city": "Chicago"}
]
  • Select all fields:

    jonq simple.json "select *"
    
  • Filter and sort:

    jonq simple.json "select name, age if age > 25 sort age desc 2"
    

    Output:

    [
      {"name": "Charlie", "age": 35},
      {"name": "Alice", "age": 30}
    ]
    
  • Distinct values:

    jonq simple.json "select distinct city"
    
  • Standalone limit:

    jonq simple.json "select * limit 2"
    
  • IN operator:

    jonq simple.json "select * if city in ('New York', 'Chicago')"
    
  • NOT operator:

    jonq simple.json "select * if not age > 30"
    
  • LIKE operator:

    jonq simple.json "select * if name like 'Al%'"
    
  • String functions:

    jonq simple.json "select upper(name) as name_upper"
    

    Output:

    [
      {"name_upper": "ALICE"},
      {"name_upper": "BOB"},
      {"name_upper": "CHARLIE"}
    ]
    
  • Math functions:

    jonq simple.json "select round(age) as rounded_age"
    
  • Count distinct:

    jonq simple.json "select count(distinct city) as unique_cities"
    
  • Aggregation with having:

    jonq simple.json "select city, avg(age) as avg_age group by city having avg_age > 25"
    

    Output:

    [
      {"city": "Chicago", "avg_age": 35},
      {"city": "New York", "avg_age": 30}
    ]
    
  • Arithmetic expression:

    jonq simple.json "select max(age) - min(age) as age_range"
    

    Output:

    {"age_range": 10}
    

Nested JSON

Consider nested.json:

[
  {
    "id": 1, "name": "Alice",
    "profile": {"age": 30, "address": {"city": "New York", "zip": "10001"}},
    "orders": [
      {"order_id": 101, "item": "Laptop", "price": 1200},
      {"order_id": 102, "item": "Phone", "price": 800}
    ]
  },
  {
    "id": 2, "name": "Bob",
    "profile": {"age": 25, "address": {"city": "Los Angeles", "zip": "90001"}},
    "orders": [
      {"order_id": 103, "item": "Tablet", "price": 500}
    ]
  }
]
  • Nested fields:

    jonq nested.json "select name, profile.address.city"
    

    Output:

    [
      {"name": "Alice", "city": "New York"},
      {"name": "Bob", "city": "Los Angeles"}
    ]
    
  • Array operations:

    jonq nested.json "select name, count(orders) as order_count"
    

    Output:

    [
      {"name": "Alice", "order_count": 2},
      {"name": "Bob", "order_count": 1}
    ]
    
  • BETWEEN operator:

    jonq nested.json "select order_id, price from [].orders if price between 700 and 1000"
    

    Output:

    [
      {"order_id": 102, "price": 800}
    ]
    
  • CONTAINS operator:

    jonq nested.json "select order_id, item from [].orders if item contains 'a'"
    

    Output:

    [
      {"order_id": 101, "item": "Laptop"},
      {"order_id": 103, "item": "Tablet"}
    ]
    
  • FROM clause over nested arrays:

    jonq nested.json "select order_id, item, price from [].orders"
    

    Output:

    [
      {"order_id": 101, "item": "Laptop", "price": 1200},
      {"order_id": 102, "item": "Phone", "price": 800},
      {"order_id": 103, "item": "Tablet", "price": 500}
    ]
    

Multiple Input Sources

  • URL fetch:

    jonq https://api.example.com/users.json "select name, email"
    
  • Glob multiple files:

    jonq 'logs/*.json' "select * if level = 'error'"
    
  • Stdin:

    cat data.json | jonq - "select name, age"
    
  • NDJSON (auto-detected):

    jonq data.ndjson "select name, age if age > 25"