About this article
This article was created using an automated generation workflow leveraging generative AI. Implementation from existing learning materials was cross-referenced with official documentation, and operational examples, constraints, and reference test results were organized.Verification Status: Partially cross-referenced with official documentation and implementation, and reference-tested. Browser behavior for the fixed demo version has not been verified.
It is difficult to understand order amounts by department just by looking at individual order lines. We will group them by department and extract only the totals.
Key terminology to know first
Expressionis a character string describing rules for extraction and aggregation.
Groupingis the process of bundling data by common items.
Thinking in Sankey diagrams
Line thickness represents the amount. Adding the two items merging into General Affairs equals 2,000.
sankey-beta 明細A,総務,800 明細B,経理,1500 明細C,総務,1200 総務,全部署,2000 経理,全部署,1500
Live demo operation
Change one of the amounts. Next, enclose that amount in quotation marks.
How to read results
If numerical, totals can be calculated by department. Strings will cause an error.
Add one more order item and predict which department's total will change. Extra whitespace in department names can also cause groups to split.
Code highlights
Evaluate items{department: $sum(amount)} to get the total per department.
The implementation can be checked in the Implementation Code section of the demo screen.
When using it at work
When importing a CSV with mixed types, normalize the amount data types before aggregation.
First, aggregate three orders
The input field contains a JSON array with three line items: General Affairs 800, Accounting 1500, and General Affairs 1200. Upon execution, we expect an object with department names as keys and summed amounts to appear in the result field. General Affairs will total 2000, and Accounting will total 1500. Compare the original three line items with the two aggregated results to confirm that the number of items has decreased. Aggregation is not about deleting data, but about summarizing information into necessary units.
| Department | Line Item Amount | Total |
|---|---|---|
| General Affairs | 800, 1200 | 2000 |
| Accounting | 1500 | 1500 |
Next, change the General Affairs 800 to 900. In the expected result, only General Affairs changes to 2100, while Accounting remains 1500. If the totals for other departments change as well, check the entered values and department names. Furthermore, adding another General Affairs line item groups rows with the same department name together. Checking whether the sum of the original line items matches the sum of the department totals after each operation makes it easier to catch aggregation errors.
Breaking down the JSONata expression
The expression in the demo is items{department: $sum(amount)}. First, items refers to the collection of line items. On the JavaScript side, JSON.parse reads the input, stores it in a property named items, and passes it to evaluate. Therefore, the JSON you write in the input field should be an array, and you do not need to add the outer object containing items yourself. Wrapping it twice will make it inconsistent with the structure the expression expects.
The first half before the curly braces, department, is the expression that creates the result keys. Rows with the same department name are gathered into a single group, and the latter half, $sum(amount), aggregates the amounts within that group. The mechanism of evaluating the value expression after this grouping process is important. It is different from simply overwriting object values with the final line item. When reading code, it is easier to understand by separately verifying which collection is targeted, what is used as the key for grouping, and what is being aggregated.
Mixing numbers and strings
If you enclose 800 in double quotes to change it to "800", the value becomes a string rather than a number. Official documentation states that non-numeric items in $sum result in errors. Because data imported from CSV may store numbers as strings, check the data types and allowable ranges prior to aggregation. Unconditionally converting empty strings to 0 or silently ignoring unconvertible amounts can cause the totals to appear correct while resulting in lost line items. Designing the workflow to verify original values and converted values separately is helpful.
You can also test by adding spaces before and after the department name. Even if they look identical, they may be treated as separate keys. In business data, using department codes rather than display names as keys can sometimes reduce the impact of name changes and textual variations. For rows with missing amounts or departments, this minimal example does not guarantee correct processing. Prepare separate input validation prior to aggregation, and define the specification as to whether missing values should cause an error or be excluded.
Points to check for practical use
When compiling purchasing data by department to generate monthly reports, determine the conditions for the target period, currency, tax handling, and cancellation details, in addition to the aggregated amounts. Cross-check the sum of the departmental totals against the total amount of the line items, and separately log excluded rows and conversion failures. While this example using a small number of integers is for learning purposes, when handling actual billing amounts, consider numerical precision and rounding rules separately.
If the JSON is invalid, JSON.parse will fail first. If the amount data types differ, it may fail during the aggregation stage. If even a normal initial value does not work, check Network and Console to verify retrieval of external modules. The absence of errors and achieving the desired amounts are separate verification items. Do not paste actual purchase details or customer names into public demos; understand the aggregation conditions using a small amount of fictional data before applying them to an authorized environment.
Verification Scope
Using Node.js 22.16.0, the same initial JSON was aggregated by department using standard JavaScript, confirming General Affairs: 2000 and Accounting: 1500. The JSONata core engine was not executed. Since the official download page indicates version 1.8.0, additional verification for the pinned version 2.0.6 remains.
The targets are current browsers and the course-specified JSONata 2.0.6. Network communication is required to fetch external CDNs and Mermaid; re-verification of actual material display, actual diagram rendering, and the pinned version license remains. Because official documentation also includes descriptions of other versions, this must be distinguished from proof of fixed-version execution.
References
Official API and Guide(Accessed November 30, 2026)
