API Reference
Welcome to the complete API reference for JSONL Algebra. Every function and class is listed here with its full documentation.
ja.core
Core relational operations for the JSONL algebra system.
This module implements the fundamental set and relational operations that form the algebra for manipulating collections of JSON objects. All operations are designed to work with lists of dictionaries, making them suitable for processing JSONL data.
collect(data)
Collect metadata-grouped rows into actual groups.
This function takes rows with _groups metadata (from groupby operations) and collects them into explicit groups. Each output row represents one group with all its members in a _rows array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries with _groups metadata |
required |
Returns:
Type | Description |
---|---|
Relation
|
List where each dict represents a group with _rows array |
Example
Input: [ {"id": 1, "region": "North", "_groups": [{"field": "region", "value": "North"}]}, {"id": 2, "region": "North", "_groups": [{"field": "region", "value": "North"}]}, {"id": 3, "region": "South", "_groups": [{"field": "region", "value": "South"}]} ]
Output: [ {"region": "North", "_rows": [{"id": 1, "region": "North"}, {"id": 2, "region": "North"}]}, {"region": "South", "_rows": [{"id": 3, "region": "South"}]} ]
Source code in ja/core.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
difference(left, right)
Compute the difference of two collections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
left
|
Relation
|
First collection |
required |
right
|
Relation
|
Second collection |
required |
Returns:
Type | Description |
---|---|
Relation
|
Elements in left but not in right |
Source code in ja/core.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
distinct(data)
Remove duplicate rows from a collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries |
required |
Returns:
Type | Description |
---|---|
Relation
|
List with duplicates removed |
Source code in ja/core.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
intersection(left, right)
Compute the intersection of two collections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
left
|
Relation
|
First collection |
required |
right
|
Relation
|
Second collection |
required |
Returns:
Type | Description |
---|---|
Relation
|
Intersection of the two collections |
Source code in ja/core.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
join(left, right, on)
Inner join with nested-key support.
Source code in ja/core.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
product(left, right)
Cartesian product; colliding keys from right are prefixed with b_
.
Source code in ja/core.py
193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
project(data, fields, use_jmespath=False)
Project specific fields from each row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries to project |
required |
fields
|
List[str] | str
|
Comma-separated field names or expressions |
required |
use_jmespath
|
bool
|
If True, use JMESPath for projection |
False
|
Returns:
Type | Description |
---|---|
Relation
|
List of dictionaries with only the specified fields |
Source code in ja/core.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
rename(data, mapping)
Rename fields in each row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries |
required |
mapping
|
Dict[str, str]
|
Dictionary mapping old names to new names |
required |
Returns:
Type | Description |
---|---|
Relation
|
List with renamed fields |
Source code in ja/core.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
select(data, expr, use_jmespath=False)
Filter rows based on an expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries to filter |
required |
expr
|
str
|
Expression to evaluate (simple expression or JMESPath) |
required |
use_jmespath
|
bool
|
If True, use JMESPath evaluation |
False
|
Returns:
Type | Description |
---|---|
Relation
|
List of rows where the expression evaluates to true |
Source code in ja/core.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
union(left, right)
Compute the union of two collections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
left
|
Relation
|
First collection |
required |
right
|
Relation
|
Second collection |
required |
Returns:
Type | Description |
---|---|
Relation
|
Union of the two collections |
Source code in ja/core.py
228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
ja.cli
Command-line interface for JSONL algebra operations.
This module provides the main CLI entry point and argument parsing for all JSONL algebra operations including relational algebra, schema inference, data import/export, and interactive REPL mode.
handle_export_command_group(args)
Handle export subcommands by delegating to appropriate handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
Parsed command line arguments with export_cmd attribute. |
required |
Source code in ja/cli.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
handle_import_command_group(args)
Handle import subcommands by delegating to appropriate handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
Parsed command line arguments with import_cmd attribute. |
required |
Source code in ja/cli.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
json_error(error_type, message, details=None, exit_code=1)
Output error in JSON format and exit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error_type
|
Type of error (e.g., "ParseError", "IOError") |
required | |
message
|
Human-readable error message |
required | |
details
|
Optional dict with additional error details |
None
|
|
exit_code
|
Exit code (default: 1) |
1
|
Source code in ja/cli.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
ja.repl
Interactive REPL (Read-Eval-Print Loop) for JSONL algebra operations.
This module provides a friendly, interactive shell for chaining JSONL algebra operations together. It's a great way to explore your data, build up complex transformation pipelines step-by-step, and see the results instantly.
Think of it as a command-line laboratory for your JSONL data!
ReplCompiler
Compiles and executes a sequence of JSONL algebra commands.
This class is the engine of the REPL. It manages the state of the command pipeline, parses user input, and translates the pipeline into a shell command that can be executed. It's designed to provide an intuitive, interactive experience for building data workflows.
Source code in ja/repl.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
__init__()
Initialize the REPL compiler with an empty pipeline.
Source code in ja/repl.py
24 25 26 27 28 |
|
add_to_pipeline(command_name, args, cli_command_name=None)
Add a new command step to the current pipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command_name
|
str
|
The name of the REPL command (e.g., "project"). |
required |
args
|
list
|
The list of arguments for the command. |
required |
cli_command_name
|
str
|
The corresponding |
None
|
Source code in ja/repl.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
handle_agg(args)
Handle the 'agg' command by adding it to the pipeline.
Source code in ja/repl.py
161 162 163 164 165 166 167 |
|
handle_compile(cmd_args)
Generate and print a bash script for the current pipeline.
Source code in ja/repl.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
handle_difference(args)
Handle the 'difference' command by adding it to the pipeline.
Source code in ja/repl.py
190 191 192 193 194 195 |
|
handle_distinct(args)
Handle the 'distinct' command by adding it to the pipeline.
Source code in ja/repl.py
131 132 133 134 135 |
|
handle_execute(cmd_args)
Execute the current pipeline and display the output.
Source code in ja/repl.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
handle_from(args)
Set the initial data source for the pipeline (e.g., a file).
This command must be the first one used when starting a new pipeline with a file source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
list
|
A list containing the file path or "stdin". |
required |
Source code in ja/repl.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
handle_groupby(args)
Handle the 'groupby' command by adding it to the pipeline.
Source code in ja/repl.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
handle_help(args)
Display the help message with all available REPL commands.
Source code in ja/repl.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
handle_intersection(args)
Handle the 'intersection' command by adding it to the pipeline.
Source code in ja/repl.py
183 184 185 186 187 188 |
|
handle_join(args)
Handle the 'join' command by adding it to the pipeline.
Source code in ja/repl.py
116 117 118 119 120 121 122 |
|
handle_pipeline_show(args)
Display the steps in the current pipeline.
Source code in ja/repl.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
handle_product(args)
Handle the 'product' command by adding it to the pipeline.
Source code in ja/repl.py
169 170 171 172 173 174 |
|
handle_project(args)
Handle the 'project' command by adding it to the pipeline.
Source code in ja/repl.py
109 110 111 112 113 114 |
|
handle_rename(args)
Handle the 'rename' command by adding it to the pipeline.
Source code in ja/repl.py
124 125 126 127 128 129 |
|
handle_reset(args)
Clear the current pipeline and reset the input source.
Source code in ja/repl.py
388 389 390 391 392 |
|
handle_select(args)
Handle the 'select' command by adding it to the pipeline.
Source code in ja/repl.py
102 103 104 105 106 107 |
|
handle_sort(args)
Handle the 'sort' command by adding it to the pipeline.
Source code in ja/repl.py
137 138 139 140 141 142 |
|
handle_union(args)
Handle the 'union' command by adding it to the pipeline.
Source code in ja/repl.py
176 177 178 179 180 181 |
|
parse_command(line)
Parse a line of input into a command and its arguments.
Uses shlex
to handle quoted arguments correctly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
str
|
The raw input line from the user. |
required |
Returns:
Type | Description |
---|---|
A tuple of (command, args_list), or (None, None) if parsing fails. |
Source code in ja/repl.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
process(line)
Process a single line of input from the REPL.
This method parses the line, finds the appropriate handler for the command, and invokes it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
str
|
The line of input to process. |
required |
Source code in ja/repl.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
|
run(initial_command_list=None)
Start the main REPL event loop.
This method prints a welcome message, registers all command handlers, and enters an infinite loop to read and process user input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_command_list
|
list
|
A list of command-line arguments to process before starting the interactive loop. |
None
|
Source code in ja/repl.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
repl(parsed_cli_args)
Entry point for the ja repl
command.
Initializes and runs the ReplCompiler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parsed_cli_args
|
Namespace
|
The parsed command-line arguments, which may include an initial file to load. |
required |
Source code in ja/repl.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
ja.commands
Command handlers for the JSONL algebra CLI.
This module connects the command-line interface to the core data processing
functions. Each handle_*
function is responsible for reading input data,
calling the appropriate core function, and writing the results to stdout.
get_input_stream(file_path)
Yield a readable file-like object.
- If file_path is None or '-', yield sys.stdin.
- Otherwise open the given path for reading.
Source code in ja/commands.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
handle_agg(args)
Handle agg command.
Source code in ja/commands.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
handle_collect(args)
Handle collect command.
Source code in ja/commands.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
|
handle_difference(args)
Handle difference command.
Source code in ja/commands.py
199 200 201 202 203 204 205 206 207 |
|
handle_distinct(args)
Handle distinct command.
Source code in ja/commands.py
210 211 212 213 214 215 216 |
|
handle_explode(args)
Handle explode command.
Source code in ja/commands.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
handle_groupby(args)
Handle groupby command.
Source code in ja/commands.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
handle_implode(args)
Handle implode command.
Source code in ja/commands.py
311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
handle_import_csv(args)
Handle import-csv command.
Source code in ja/commands.py
326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
handle_intersection(args)
Handle intersection command.
Source code in ja/commands.py
188 189 190 191 192 193 194 195 196 |
|
handle_join(args)
Handle join command.
Source code in ja/commands.py
129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
handle_product(args)
Handle product command.
Source code in ja/commands.py
144 145 146 147 148 149 150 151 152 |
|
handle_project(args)
Handle project command.
Source code in ja/commands.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
handle_rename(args)
Handle rename command.
Source code in ja/commands.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
handle_schema_infer(args)
Handle schema infer command.
Source code in ja/commands.py
268 269 270 271 272 273 274 |
|
handle_schema_validate(args)
Handle schema validate command.
Source code in ja/commands.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
handle_select(args)
Handle select command.
Source code in ja/commands.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
handle_sort(args)
Handle sort command.
Source code in ja/commands.py
219 220 221 222 223 224 225 |
|
handle_to_array(args)
Handle to-array command.
Source code in ja/commands.py
277 278 279 280 281 |
|
handle_to_csv(args)
Handle to-csv command.
Source code in ja/commands.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
handle_to_jsonl(args)
Handle to-jsonl command.
Source code in ja/commands.py
284 285 286 287 288 289 290 291 292 |
|
handle_union(args)
Handle union command.
Source code in ja/commands.py
177 178 179 180 181 182 183 184 185 |
|
json_error(error_type, message, details=None)
Print a JSON error message to stderr and exit.
Source code in ja/commands.py
78 79 80 81 82 83 84 85 86 87 88 89 |
|
read_jsonl(input_stream)
Read JSONL data from a file-like object.
Source code in ja/commands.py
62 63 64 |
|
write_json_object(obj)
Write a single object as pretty-printed JSON to stdout.
Source code in ja/commands.py
73 74 75 |
|
write_jsonl(rows)
Write a collection of objects as JSONL to stdout.
Source code in ja/commands.py
67 68 69 70 |
|
ja.group
Grouping operations for JSONL algebra.
This module provides grouping functionality that supports both immediate aggregation and metadata-based chaining for multi-level grouping.
groupby_agg(data, group_key, agg_spec)
Group and aggregate in one operation.
This function is kept for backward compatibility and for the --agg flag. It's more efficient for simple cases but less flexible than chaining.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries to group and aggregate |
required |
group_key
|
str
|
Field to group by |
required |
agg_spec
|
str
|
Aggregation specification |
required |
Returns:
Type | Description |
---|---|
Relation
|
List of aggregated results, one per group |
Source code in ja/group.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
groupby_chained(grouped_data, new_group_key)
Apply groupby to already-grouped data.
This function handles multi-level grouping by building on existing group metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grouped_data
|
Relation
|
Data with existing group metadata |
required |
new_group_key
|
str
|
Field to group by |
required |
Returns:
Type | Description |
---|---|
Relation
|
List with nested group metadata |
Source code in ja/group.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
groupby_with_metadata(data, group_key)
Group data and add metadata fields.
This function enables chained groupby operations by adding special metadata fields to each row: - _groups: List of {field, value} objects representing the grouping hierarchy - _group_size: Total number of rows in this group - _group_index: This row's index within its group
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries to group |
required |
group_key
|
str
|
Field to group by (supports dot notation) |
required |
Returns:
Type | Description |
---|---|
Relation
|
List with group metadata added to each row |
Source code in ja/group.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
ja.agg
Aggregation engine for JSONL algebra operations.
This module provides all aggregation functionality including parsing aggregation specifications, applying aggregations to data, and all built-in aggregation functions (sum, avg, min, max, etc.).
aggregate_grouped_data(grouped_data, agg_spec)
Aggregate data that has group metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grouped_data
|
Relation
|
Data with group metadata |
required |
agg_spec
|
str
|
Aggregation specification |
required |
Returns:
Type | Description |
---|---|
Relation
|
List of aggregated results |
Source code in ja/agg.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
aggregate_single_group(data, agg_spec)
Aggregate ungrouped data as a single group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Relation
|
List of dictionaries |
required |
agg_spec
|
str
|
Aggregation specification |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with aggregation results |
Source code in ja/agg.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
apply_single_agg(spec, data)
Apply a single aggregation to data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spec
|
Tuple[str, str]
|
(name, expression) tuple |
required |
data
|
Relation
|
List of dictionaries |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with aggregation result |
Source code in ja/agg.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
parse_agg_specs(agg_spec)
Parse aggregation specification string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agg_spec
|
str
|
Aggregation specification (e.g., "count, avg_age=avg(age)") |
required |
Returns:
Type | Description |
---|---|
List[Tuple[str, str]]
|
List of (name, expression) tuples |
Source code in ja/agg.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
ja.export
Utilities for exporting JSONL data to other formats.
This module provides a collection of functions for converting JSONL data into
various other formats. It powers the ja export
command group, enabling
transformations like converting JSONL to a standard JSON array or "exploding"
a JSONL file into a directory of individual JSON files.
dir_to_jsonl(input_dir_path_str, add_filename_key=None, recursive=False)
Converts JSON files in a directory to JSONL lines.
Files are sorted by 'item-
Source code in ja/export.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
json_array_to_jsonl_lines(json_array_input_stream)
Read a JSON array from a stream and yield each element as a JSONL line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_array_input_stream
|
Input stream containing a JSON array. |
required |
Yields:
Type | Description |
---|---|
JSON strings representing each array element. |
Raises:
Type | Description |
---|---|
ValueError
|
If the input is not a valid JSON array. |
Source code in ja/export.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
jsonl_to_dir(jsonl_input_stream, output_dir_path_str, input_filename_stem='data')
Exports JSONL lines to individual JSON files in a directory.
The output directory is named after input_filename_stem if output_dir_path_str is not specific.
Files are named item-
Source code in ja/export.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
jsonl_to_json_array_string(jsonl_input_stream)
Read JSONL from a stream and return a JSON array string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonl_input_stream
|
Input stream containing JSONL data. |
required |
Returns:
Type | Description |
---|---|
str
|
A JSON array string containing all records. |
Source code in ja/export.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
ja.exporter
Export your JSONL data to other popular formats like CSV.
This module is your gateway to the wider data ecosystem. It provides powerful and flexible tools to convert your JSONL data into formats that are easy to use with spreadsheets, traditional databases, or other data analysis tools.
The key feature is its intelligent handling of nested JSON, which can be "flattened" into separate columns, making complex data accessible in a simple CSV format.
jsonl_to_csv_stream(jsonl_stream, output_stream, flatten=True, flatten_sep='.', column_functions=None)
Convert a stream of JSONL data into a CSV stream.
This is a highly flexible function for exporting your data. It reads JSONL records, intelligently discovers all possible headers (even if they vary between lines), and writes to a CSV format.
It shines when dealing with nested data. By default, it will flatten
structures like {"user": {"name": "X"}}
into a user.name
column.
You can also provide custom functions to transform data on the fly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsonl_stream
|
An input stream (like a file handle) yielding JSONL strings. |
required | |
output_stream
|
An output stream (like |
required | |
flatten
|
bool
|
If |
True
|
flatten_sep
|
str
|
The separator to use when flattening keys. Defaults to ".". |
'.'
|
column_functions
|
dict
|
A dictionary mapping column names to functions
that will be applied to that column's data
before writing to CSV. For example,
|
None
|
Source code in ja/exporter.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
ja.importer
Import data from other formats like CSV into the world of JSONL.
This module is the bridge that brings your existing data into the JSONL Algebra
ecosystem. It provides a collection of powerful functions for converting various
data formats—such as CSV or directories of individual JSON files—into the clean,
line-oriented JSONL format that ja
is built to handle.
csv_to_jsonl_lines(csv_input_stream, has_header, infer_types=False)
Convert a stream of CSV data into a stream of JSONL lines.
This function reads CSV data and transforms each row into a JSON object. It can automatically handle headers to use as keys and can even infer the data types of your values, converting them from strings to numbers or booleans where appropriate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_input_stream
|
An input stream (like a file handle) containing CSV data. |
required | |
has_header
|
bool
|
Set to |
required |
infer_types
|
bool
|
If |
False
|
Yields:
Type | Description |
---|---|
A JSON-formatted string for each row in the CSV data. |
Source code in ja/importer.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
dir_to_jsonl_lines(dir_path)
Stream a directory of .json or .jsonl files as a single JSONL stream.
A handy utility for consolidating data. It reads all files ending in .json
or .jsonl
from a specified directory and yields each JSON object as a
separate line. This is perfect for preparing a dataset that has been
stored as many small files.
- For
.json
files, the entire file is treated as a single JSON object. - For
.jsonl
files, each line is treated as a separate JSON object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
The path to the directory to read. |
required |
Yields:
Type | Description |
---|---|
A string for each JSON object found, ready for processing. |
Source code in ja/importer.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
ja.schema
Discover the structure of your data automatically.
This module provides powerful tools to infer a JSON Schema from your JSONL files. A schema acts as a blueprint for your data, describing its fields, types, and which fields are required. This is incredibly useful for validation, documentation, and ensuring data quality.
add_required_fields(schema, data_samples)
Refine a schema by identifying which fields are always present.
This function analyzes a list of data samples and updates the schema to mark fields as 'required' if they appear in every single sample. This process is applied recursively to nested objects, making the resulting schema more precise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
The schema dictionary to modify in place. |
required | |
data_samples
|
A list of data samples to analyze for required fields. |
required |
Example
If all samples in data_samples
have 'name' and 'age' fields, this
function adds {"required": ["age", "name"]}
to the schema.
Source code in ja/schema.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
get_json_type(value)
Determine the appropriate JSON Schema type for a given Python value.
Maps Python types to their corresponding JSON Schema type names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any Python value. |
required |
Returns:
Type | Description |
---|---|
The JSON Schema type name as a string. |
Example
get_json_type("hello") 'string' get_json_type(42) 'integer'
Source code in ja/schema.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
infer_schema(data)
Infer a complete JSON schema from a collection of data records.
This is the main entry point for schema inference. Give it an iterable of JSON objects (like a list of dictionaries), and it will return a complete JSON Schema that describes the entire dataset. It automatically handles varying fields, mixed types, nested structures, and identifies required fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
An iterable of data records (typically dictionaries). |
required |
Returns:
Type | Description |
---|---|
A JSON schema dictionary with |
Example
data = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}] schema = infer_schema(data) schema["properties"]["name"] {'type': 'string'} schema["required"]['age', 'name']
Source code in ja/schema.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
infer_value_schema(value)
Infer a JSON Schema for a single Python value.
Creates a schema that describes the structure and type of the given value, handling nested objects and arrays recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any JSON-serializable Python value. |
required |
Returns:
Type | Description |
---|---|
A JSON schema dictionary describing the value. |
Example
infer_value_schema({"name": "Alice", "age": 30}) {'type': 'object', 'properties': {'name': {'type': 'string'}, 'age': {'type': 'integer'}}}
Source code in ja/schema.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
merge_schemas(s1, s2)
Intelligently merge two JSON schemas into one.
This is the secret sauce that allows schema inference to work across many different JSON objects, even if they have different fields or types. It handles type unions (e.g., a field that is sometimes a string, sometimes an integer) and recursively merges nested object properties and array item schemas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s1
|
First JSON schema dictionary or None. |
required | |
s2
|
Second JSON schema dictionary or None. |
required |
Returns:
Type | Description |
---|---|
A merged schema dictionary combining both inputs. |
Example
s1 = {"type": "string"} s2 = {"type": "integer"} merge_schemas(s1, s2) {'type': ['integer', 'string']}
Source code in ja/schema.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
ja.expr
Expression parser for ja commands.
This module provides a lightweight expression parser that allows intuitive syntax without quotes for most common cases.
ExprEval
Parse and evaluate expressions for filtering, comparison, and arithmetic.
Source code in ja/expr.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
evaluate(expr, context)
Parse and evaluate an expression.
Examples:
"status == active" "age > 30" "user.type == premium"
Source code in ja/expr.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
evaluate_arithmetic(expr, context)
Evaluate simple arithmetic expressions.
Examples:
"amount * 1.1" "score + bonus"
Source code in ja/expr.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
evaluate_comparison(left, op_str, right)
Evaluate a comparison operation.
Source code in ja/expr.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
get_field_value(obj, field_path)
Get value from nested object using dot notation.
Examples:
get_field_value({"user": {"name": "Alice"}}, "user.name") -> "Alice" get_field_value({"items": [{"id": 1}]}, "items[0].id") -> 1
Source code in ja/expr.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
parse_value(value_str)
Parse a value string into appropriate Python type.
Examples:
"123" -> 123 "12.5" -> 12.5 "true" -> True "false" -> False "null" -> None "active" -> "active" (string)
Source code in ja/expr.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
set_field_value(obj, field_path, value)
Set value in nested object using dot notation.
Source code in ja/expr.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|