求助:如何编写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
costfor product prices, but you're checking$y.price - Incorrect traversal:
$x.product()treatsproductas a function, but it's actually an array property—you need to iterate over$x.productdirectly - Type mismatch: You're comparing against the string
"3.0"instead of the numeric value3, which causes invalid type comparison errors - Missing nested iteration: You need to loop through individual products inside each
visitentry, 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
- Nested loops: The inner
for $product in $visit.productiterates through every product in each visit entry - Correct field check: Uses
$product.costto match your JSON structure - Numeric comparison: Compares against the numeric value
3to avoid type errors - Automatic filtering: Products without a
costfield (like the planning items "Pen" and "Paper") are automatically excluded, since$product.costwill not exist and fail the>=3condition
When you run this on try.zorba.io, you'll get your expected output:"Kit Kat", "Coca Cola", "Apple"
内容的提问来源于stack exchange,提问作者dsong98
相关产品推荐
相关产品推荐

