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.
Classes¶
Functions¶
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
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
join(left, right, on, how='inner')
¶
Join two relations with support for multiple join types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Relation
|
Left relation (list of dictionaries) |
required |
right
|
Relation
|
Right relation (list of dictionaries) |
required |
on
|
List[Tuple[str, str]]
|
List of (left_key, right_key) tuples specifying join columns |
required |
how
|
str
|
Join type - "inner", "left", "right", "outer", or "cross" - inner: Only matching rows from both sides (default) - left: All rows from left, matching rows from right (nulls if no match) - right: All rows from right, matching rows from left (nulls if no match) - outer: All rows from both sides (nulls where no match) - cross: Cartesian product (ignores 'on' parameter) |
'inner'
|
Returns:
| Type | Description |
|---|---|
Relation
|
Joined relation as list of dictionaries |
Examples:
>>> left = [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
>>> right = [{"user_id": 1, "order": "Book"}]
>>> join(left, right, [("id", "user_id")], how="left")
[{"id": 1, "name": "Alice", "order": "Book"},
{"id": 2, "name": "Bob", "order": None}]
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 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 | |
product(left, right)
¶
Cartesian product; colliding keys from right are prefixed with b_.
Source code in ja/core.py
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
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 |
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
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
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
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
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.
Functions¶
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
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
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
ja.repl
¶
Interactive REPL (Read-Eval-Print Loop) for JSONL algebra operations.
This module provides a powerful, interactive shell for JSONL data manipulation with named datasets, immediate execution, and a non-destructive design.
Key features:
- Named datasets: Load and manage multiple JSONL files by name
- Safe operations: All transformations require unique output names
- Immediate execution: See results right away, no pipeline building
- File-based streaming: Store paths, not data (memory efficient)
- Shell integration: Execute bash commands with !
Classes¶
ReplSession
¶
Interactive REPL session for JSONL data manipulation.
This class manages a workspace of named datasets (JSONL files) and provides commands for loading, transforming, and exploring data interactively.
Design principles: - Non-destructive: Operations create new datasets, never modify originals - Explicit: All operations require unique output names - Streaming: Store file paths, not data in memory
Source code in ja/repl.py
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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 | |
Functions¶
__init__()
¶
Initialize the REPL session.
Source code in ja/repl.py
handle_load(args)
¶
Load a JSONL file into the workspace.
Usage: load
If name is not provided, uses the file stem (filename without extension). The loaded dataset becomes the current dataset.
Source code in ja/repl.py
handle_cd(args)
¶
Switch to a different dataset.
Usage: cd
Source code in ja/repl.py
handle_pwd(args)
¶
Show the current dataset name and path.
Usage: pwd Alias: current
Source code in ja/repl.py
handle_current(args)
¶
handle_datasets(args)
¶
List all registered datasets.
Usage: datasets
Shows all loaded datasets with a marker for the current one.
Source code in ja/repl.py
handle_save(args)
¶
Save the current dataset to a file.
Usage: save
Writes the current dataset to the specified file path. Does not register the file as a new dataset.
Source code in ja/repl.py
handle_ls(args)
¶
Preview a dataset.
Usage: ls [name][--limit N]
Shows the first N lines of the dataset (default: window_size setting). If name is omitted, shows the current dataset.
Source code in ja/repl.py
handle_shell(args)
¶
Execute a shell command.
Usage: !
Passes the command directly to the shell.
Source code in ja/repl.py
handle_window_size(args)
¶
Get or set the window size setting.
Usage: window-size [N]
Without arguments, shows the current value. With a number, sets the value.
Source code in ja/repl.py
handle_info(args)
¶
Show statistics and information about a dataset.
Usage: info [name]
If name is omitted, shows info for the current dataset. Displays: row count, file size, fields, and a sample row.
Source code in ja/repl.py
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 | |
handle_select(args)
¶
Filter rows with an expression.
Usage: select '
Creates a new dataset with filtered rows.
Source code in ja/repl.py
handle_project(args)
¶
Select specific fields.
Usage: project
Creates a new dataset with only the specified fields.
Source code in ja/repl.py
handle_rename(args)
¶
Rename fields.
Usage: rename
Example: rename old=new,foo=bar output
Source code in ja/repl.py
handle_distinct(args)
¶
Remove duplicate rows.
Usage: distinct
Source code in ja/repl.py
handle_sort(args)
¶
Sort rows by key(s).
Usage: sort
Example: sort age,name output Example: sort age --desc output
Source code in ja/repl.py
handle_groupby(args)
¶
Group rows by a key.
Usage: groupby
Example: groupby region output Example: groupby region --agg count,sum(amount) output
Source code in ja/repl.py
handle_join(args)
¶
Join with another dataset.
Usage: join
Example: join orders --on user_id=id user_orders
Source code in ja/repl.py
handle_union(args)
¶
Union with another dataset.
Usage: union
Source code in ja/repl.py
handle_intersection(args)
¶
Intersection with another dataset.
Usage: intersection
Source code in ja/repl.py
handle_difference(args)
¶
Difference with another dataset.
Usage: difference
Source code in ja/repl.py
handle_product(args)
¶
Cartesian product with another dataset.
Usage: product
Source code in ja/repl.py
handle_help(args)
¶
Display help message.
Source code in ja/repl.py
parse_command(line)
¶
Parse a command line into command and arguments.
Source code in ja/repl.py
process(line)
¶
Process a single command line.
Source code in ja/repl.py
run(initial_args=None)
¶
Run the REPL main loop.
Source code in ja/repl.py
Functions¶
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.
Functions¶
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
read_jsonl(input_stream)
¶
write_jsonl(rows)
¶
write_json_object(obj)
¶
json_error(error_type, message, details=None)
¶
Print a JSON error message to stderr and exit.
Source code in ja/commands.py
handle_select(args)
¶
Handle select command.
Source code in ja/commands.py
handle_project(args)
¶
Handle project command.
Source code in ja/commands.py
handle_join(args)
¶
Handle join command.
Source code in ja/commands.py
handle_product(args)
¶
Handle product command.
Source code in ja/commands.py
handle_rename(args)
¶
Handle rename command.
Source code in ja/commands.py
handle_union(args)
¶
Handle union command.
Source code in ja/commands.py
handle_intersection(args)
¶
Handle intersection command.
Source code in ja/commands.py
handle_difference(args)
¶
Handle difference command.
Source code in ja/commands.py
handle_distinct(args)
¶
handle_sort(args)
¶
handle_groupby(args)
¶
Handle groupby command.
Source code in ja/commands.py
handle_agg(args)
¶
Handle agg command.
Source code in ja/commands.py
handle_schema_infer(args)
¶
handle_to_array(args)
¶
handle_to_jsonl(args)
¶
Handle to-jsonl command.
Source code in ja/commands.py
handle_explode(args)
¶
Handle explode command.
Source code in ja/commands.py
handle_implode(args)
¶
Handle implode command.
Source code in ja/commands.py
handle_import_csv(args)
¶
Handle import-csv command.
Source code in ja/commands.py
handle_to_csv(args)
¶
Handle to-csv command.
Source code in ja/commands.py
handle_schema_validate(args)
¶
Handle schema validate command.
Source code in ja/commands.py
handle_collect(args)
¶
Handle collect command.
Source code in ja/commands.py
handle_window(args)
¶
Handle window function command.
Source code in ja/commands.py
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 | |
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.
Classes¶
Functions¶
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
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
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
|
Union[str, List[Tuple[str, str]]]
|
Aggregation specification |
required |
Returns:
| Type | Description |
|---|---|
Relation
|
List of aggregated results, one per group |
Source code in ja/group.py
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.).
Classes¶
Functions¶
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
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
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 | |
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
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
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.
Functions¶
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
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
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
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
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.
Functions¶
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
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.
Functions¶
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
.jsonfiles, the entire file is treated as a single JSON object. - For
.jsonlfiles, 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
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
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.
Functions¶
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
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)
Source code in ja/schema.py
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
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
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
ja.expr
¶
Expression parser for ja commands.
This module provides a lightweight expression parser that allows intuitive syntax without quotes for most common cases.
Classes¶
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 | |
Functions¶
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
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
set_field_value(obj, field_path, value)
¶
Set value in nested object using dot notation.
Source code in ja/expr.py
evaluate_comparison(left, op_str, right)
¶
Evaluate a comparison operation.
Source code in ja/expr.py
evaluate(expr, context)
¶
Parse and evaluate an expression.
Examples:
"status == active" "age > 30" "user.type == premium"
Source code in ja/expr.py
evaluate_arithmetic(expr, context)
¶
Evaluate simple arithmetic expressions.
Examples:
"amount * 1.1" "score + bonus"