An orders API might return everything it knows about an order, while the service calling it needs a smaller response. In this example, the caller needs paid orders, two renamed order fields, and only the SKU and quantity for each line item.
The input has two orders under data.orders. A102 is paid and A103 is canceled. Each line item also has a name that the caller does not need.
Write the mapping
response as r { orders: r.data.orders as o [{ orderId: o.id, total: o.amount, items: o.items [{ sku, quantity }] }] where o.status = "PAID" } The outer braces produce one object. The orders field reads r.data.orders and uses [{…}] to produce an array. The where clause filters that array by status. Inside each result, orderId: o.id and total: o.amount give the fields their output names. The inner projection reads sku and quantity from each item.
Run it with JDK 21 and Maven
Clone the MosaicQL source. From the repo root, build the library and write its dependency classpath to a file. These commands use a Unix-style shell.
mvn -q -pl mosaicql-parser -am -DskipTests package mvn -q -pl mosaicql-parser dependency:build-classpath \ -Dmdep.outputFile=target/example-classpath.txt Save the following as PaidOrders.java in the repo root. The host application supplies the JSON as a named source, so the key response matches the first word of the query.
import com.mosaicql.MosaicQL; import com.mosaicql.runtime.ExecutionOptions; import com.mosaicql.runtime.FunctionRegistry; import com.mosaicql.runtime.InMemorySourceProvider; import com.mosaicql.runtime.JsonAdapter; import java.util.Map; public class PaidOrders { public static void main(String[] args) { var json = """ { "data": { "orders": [ { "id": "A102", "status": "PAID", "amount": 39.5, "items": [{"sku": "PEN", "quantity": 2, "name": "Pen"}] }, { "id": "A103", "status": "CANCELED", "amount": 15, "items": [{"sku": "PAD", "quantity": 1, "name": "Notepad"}] } ] } } """; var query = """ response as r { orders: r.data.orders as o [{ orderId: o.id, total: o.amount, items: o.items [{ sku, quantity }] }] where o.status = "PAID" } """; var sources = new InMemorySourceProvider( Map.of("response", JsonAdapter.read(json)) ); var result = MosaicQL.execute( query, sources, new FunctionRegistry(), new ExecutionOptions(1000, 100, 16) ); System.out.println(JsonAdapter.write(result)); } } Run the example.
java --class-path "mosaicql-parser/target/classes:$(cat mosaicql-parser/target/example-classpath.txt)" PaidOrders.java The program prints the following JSON.
{"orders":[{"orderId":"A102","total":39.5,"items":[{"sku":"PEN","quantity":2}]}]} For another check, change the query filter to "CANCELED" and rerun the program. The response will contain A103 and its PAD line item. This gives you a small fixture for checking the filter and the output shape independently.
What is the trickiest nested API response you've had to reshape? A small, anonymized input and expected output would make a good follow-up example.
AI disclosure: AI drafted this walkthrough from the author's original example. The Java program and output were verified with JDK 21.
