Skip to content

chore: find the SKU in stock.json with Groovy instead of a jsonpath filter - #95

Merged
davsclaus merged 1 commit into
mainfrom
quick-fix/stock-lookup-groovy
Oct 2, 2026
Merged

davsclaus merged 1 commit into
mainfrom
quick-fix/stock-lookup-groovy

Conversation

@davsclaus

Copy link
Copy Markdown
Contributor

Four examples look up one SKU in stock.json: connect/stock-api, connect/http-client, contracts/openapi-server and ai/mcp-server. They did it with a JsonPath filter, $[?(@.sku == '${header.sku}')] with resultType: java.util.List, then checked the size and took ${body[0]}.

They now unmarshal the file and find the item with one line of Groovy:

- unmarshal:
    json:
      library: Jackson
- setBody:
    expression:
      groovy:
        expression: "body.find { it.sku == headers.sku }"

That gives the item as a map, or null, so the choice checks ${body} == null and the ${body[0]} step goes. Groovy reads more plainly than the filter syntax, and models write it well. In local-model benchmark runs the model repeatedly dropped the size-check-and-unwrap step after the JsonPath filter.

READMEs, the teaches metadata and camel-jbang-example-catalog.json (regenerated) are updated to match. transform/json-transform keeps JsonPath because it teaches jsonpath and jq. The connect-service examples only read single fields ($.orderId) and are unchanged.

Tested:

  • camel test run passes for all four examples on 4.23.0-SNAPSHOT
  • connect/stock-api on --camel-version=4.22.1: curl /stock/CAMEL-MUG returns 200 with the item, /stock/CAMEL-SOCKS returns 404 with unknown sku CAMEL-SOCKS

🤖 Generated with Claude Code

https://claude.ai/code/session_01STT6whBgK1AqsSsUKrnE8m

…ilter

stock-api, http-client, openapi-server and mcp-server looked up one SKU with
a jsonpath filter returning a list, then a size check and ${body[0]}. They now
unmarshal the file and run body.find { it.sku == headers.sku }, which gives
the item or null in one line. READMEs, metadata and the catalog follow.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01STT6whBgK1AqsSsUKrnE8m
@davsclaus
davsclaus merged commit 865199f into main Oct 2, 2026
34 checks passed
@davsclaus
davsclaus deleted the quick-fix/stock-lookup-groovy branch October 2, 2026 18:11
davsclaus added a commit to apache/camel that referenced this pull request Oct 2, 2026
…ovy instead of a jsonpath filter

Synced with the sync-example-catalog profile after apache/camel-jbang-examples#95:
stock-api, http-client and openapi-server unmarshal stock.json and find the
SKU with body.find { it.sku == headers.sku }, the item or null.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01STT6whBgK1AqsSsUKrnE8m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant