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 *"
    

    Output:

    [
      {"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"}
    ]
    
  • 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}
    ]
    
  • 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"}
    ]
    

Complex JSON¶

Consider complex.json (abbreviated):

{
  "company": {
    "subsidiaries": [
      {"name": "TechCorp Asia", "employees": 250, "financials": {"revenue": 42000000}},
      {"name": "TechCorp Europe", "employees": 300, "financials": {"revenue": 58000000}}
    ]
  },
  "products": [
    {"id": "P001", "type": "Software", "versions": [{"pricing": {"monthly": 199.99}}]},
    {"id": "P002", "type": "Software", "versions": [{"pricing": {"monthly": 149.99}}]}
  ]
}
  • Filtering:

    jonq complex.json "select name, founded from company.subsidiaries[] if founded > 2008"
    

    Output:

    [
      {"name": "TechCorp Asia", "founded": 2010},
    ]
    
  • Deep nesting:

    jonq complex.json "select company.headquarters.coordinates.latitude"
    

    Output:

    {
      "latitude": 37.7749
    }
    
  • Complex grouping:

    jonq complex.json "select type, avg(versions[].pricing.monthly) as avg_price group by type from products[]"
    

    Output:

    [
      {"type": "Software", "avg_price": 216.67}
    ]