问题是编写一个JSONiq FLWOR表达式,该表达式可以显示价格至少为3的产品名称。
我已经尝试过How to run JSONiq from JSON with try.zorba.io上提供的答案,但这不是我期望的答案。除此之外,我还尝试了很多JSON FLWOR表达式,但在try.zobia.io中仍然遇到错误。这是我的JSON文件。
{
"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"
}
]
}
]
}
}
这是我当前的JSONiq表达式。
jsoniq version "1.0";
let $a := { (: my JSON file :) }
for $x in $a.supermarket.visit
let $y = $x.product()
where $y.price >= "3.0"
return $y.name
最终输出应为Kit Kat
,Coca Cola
和Apple
。我会为我的JSON文件或JSONiq提供一些帮助。
答案 0 :(得分:1)
visit
也是一个数组,因此您需要使用括号才能到达for
中的各个访问。这个
jsoniq version "1.0";
let $data := { (: my JSON file :) }
for $visit in $data.supermarket.visit()
for $product in $visit.product()
where $product.cost ge 3
return $product.name
会返回
Kit Kat Coca Cola Apple
由于上面产生了一个序列,因此可以在允许序列的任何地方使用它。
let $data := { (: my JSON file :) }
return string-join(
for $visit in $data.supermarket.visit()
for $product in $visit.product()
where $product.cost ge 3
return $product.name
, ", ")
结果:
Kit Kat, Coca Cola, Apple
当然也可以:
for $product in $data.supermarket.visit().product()
where $product.cost ge 3
return $product.name