jt - JSONata Query Tool
jt is a powerful command-line tool for querying and transforming JSON data using JSONata, a lightweight query and transformation language for JSON.
Features
- 🔍 Query JSON, YAML, CSV, and JSON Lines data using JSONata expressions
- 🔄 Format conversion between JSON, YAML, JSON Lines, and CSV (no query required)
- 📝 Multiple output formats: JSON (with compact option), JSON Lines, YAML, and CSV
- 🚀 Fast and efficient processing with streaming support
- đź’ˇ User-friendly error messages with helpful suggestions
- đź“– Support for both stdin and file input
- 🎯 TypeScript implementation with full type safety
Installation
Using npm
npm install -g @2017takeda/jt-cli
Using Homebrew (coming soon)
brew install jt
Usage
Basic Usage
jt '<jsonata-expression>' input.json
cat data.json | jt '<jsonata-expression>'
jt -i yaml '<jsonata-expression>' data.yaml
jt data.json -o yaml
cat data.yaml | jt -c
The JSONata expression is now optional. When omitted, jt acts as a format converter, parsing the input and outputting it in the specified format.
Examples
Format Conversion (No Query)
jt data.json -o yaml
jt config.yaml -o json
cat data.json | jt -c
jt events.jsonl -o yaml
Basic Query
echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]' | jt '$.name'
Filtering
echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]' | jt '$[age > 25]'
Transformation
echo '{"users": [{"first": "John", "last": "Doe"}]}' | jt 'users.{"fullName": first & " " & last}'
Aggregation
echo '[{"value": 10}, {"value": 20}, {"value": 30}]' | jt '$sum(value)'
Input Formats
jt supports multiple input formats:
- JSON (default): Standard JSON format
- YAML: YAML format (
-i yaml or --input yaml)
- JSON Lines: Newline-delimited JSON (
-i jsonl or --input jsonl)
- CSV: Comma-separated values with headers (
-i csv or --input csv)
jt -i yaml '$.users.name' config.yaml
jt -i jsonl '$.event' events.jsonl
jt -i csv '$[age > 25]' users.csv
Output Formats
Control output formatting with the -o or --output option:
- json (default): Formatted JSON with indentation
- Use
-c or --compact flag for minified JSON
- jsonl: JSON Lines (one JSON per line)
- yaml: YAML format
- csv: CSV format (for tabular data)
jt -c '$.users' data.json
jt -o yaml '$.config' settings.json
jt -o csv '$' users.json
Raw String Output
Use the -r or --raw-string flag to output raw strings without quotes:
echo '{"name": "Alice", "age": 30}' | jt -r '$.name'
echo '{"count": 42, "active": true}' | jt -r '$.count'
echo '{"text": "Line 1\nLine 2"}' | jt -r '$.text'
echo '{"list": [1, 2, 3]}' | jt -r '$.list'
This option is useful for:
- Shell scripting where you need unquoted values
- Extracting text content without JSON formatting
- Similar to
jq -r for those familiar with that tool
Advanced Features
Using JSONata Functions
echo '{"name": "john doe"}' | jt '{"name": $uppercase(name)}'
echo '{"date": "2023-12-01"}' | jt '{"year": $substring(date, 0, 4)}'
Complex Queries
echo '[{"dept": "sales", "salary": 50000}, {"dept": "sales", "salary": 60000}]' | \
jt 'dept{dept: $sum(salary)}'
JSONata Expression Language
JSONata is a powerful query language designed specifically for JSON. Key features include:
- Path expressions:
$.users[0].name
- Filtering:
$.users[age > 21]
- Mapping:
$.users.{"fullName": firstName & " " & lastName}
- Aggregation:
$sum($.items.price)
- Functions:
$uppercase(), $substring(), $now(), etc.
For complete JSONata documentation, visit jsonata.org.
Error Handling
jt provides clear, actionable error messages:
echo '{invalid}' | jt '$'
echo '{}' | jt '$undefined('
Development
Prerequisites
- Node.js 20 or higher (Node.js 24 recommended)
- npm or yarn
- nvm (recommended for managing Node.js versions)
Setup
git clone https://github.com/TAKEDA-Takashi/jt-cli.git
cd jt-cli
nvm use
npm install
npm test
npm run build
Testing
This project follows Test-Driven Development (TDD) practices:
npm test
npm run test:watch
npm run test:coverage
Contributing
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Write tests for your changes
- Implement your changes
- Ensure all tests pass
- Commit your changes (
git commit -m 'feat: add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Code Ownership
This project uses CODEOWNERS to define responsibilities for different parts of the codebase. Code owners are automatically requested for review when pull requests modify files they own.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Support