如何使用 JSONiq for JSON



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

我已经尝试了如何在 JSON 上运行 JSONiq 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 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

会回来

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

最新更新