如何将 JSONiq 用于 JSON

How to use JSONiq for JSON

问题是编写一个 JSONiq FLWOR 表达式,可以显示价格至少为 3 的产品名称。

我已经尝试了 上提供的答案,但这不是我期望的答案。除此之外,我还尝试了很多 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 KatCoca ColaApple。我希望能为我的 JSON 文件或 JSONiq.

提供一些帮助

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

会return

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