You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何编写JSONiq FLWOR表达式筛选价格≥3的产品名称

Fixing JSONiq FLWOR Expression to Filter Products with Cost ≥3

Let's fix your JSONiq FLWOR step by step—you're close, but there are a few key issues tripping you up:

Key Problems in Your Original Expression

  • Wrong field name: Your JSON uses cost for product prices, but you're checking $y.price
  • Incorrect traversal: $x.product() treats product as a function, but it's actually an array property—you need to iterate over $x.product directly
  • Type mismatch: You're comparing against the string "3.0" instead of the numeric value 3, which causes invalid type comparison errors
  • Missing nested iteration: You need to loop through individual products inside each visit entry, not just the visits themselves

Corrected JSONiq Expression

jsoniq version "1.0";
let $data := { 
  "supermarket": { 
    "visit": [ 
      { "_type": "bought", "date": "March 8th, 2019", "product": [ 
        { "name": "Kit Kat", "amount": 3, "cost": 3.5 }, 
        { "name": "Coca Cola", "amount": 2, "cost": 3 }, 
        { "name": "Apple", "amount": "Some", "cost": 5.9 } 
      ] }, 
      { "_type": "planning", "product": [{ "name": "Pen", "amount": 2 }, { "name": "Paper", "amount": "One ream" } ] } 
    ] 
  } 
}
for $visit in $data.supermarket.visit
for $product in $visit.product
where $product.cost >= 3
return $product.name

What This Does

  1. Nested loops: The inner for $product in $visit.product iterates through every product in each visit entry
  2. Correct field check: Uses $product.cost to match your JSON structure
  3. Numeric comparison: Compares against the numeric value 3 to avoid type errors
  4. Automatic filtering: Products without a cost field (like the planning items "Pen" and "Paper") are automatically excluded, since $product.cost will not exist and fail the >=3 condition

When you run this on try.zorba.io, you'll get your expected output:
"Kit Kat", "Coca Cola", "Apple"

内容的提问来源于stack exchange,提问作者dsong98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:33:40