【问题标题】:How to use JSONiq for JSON如何将 JSONiq 用于 JSON
【发布时间】:2019-03-30 09:22:27
【问题描述】:

问题是编写一个 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 KatCoca ColaApple。对于我的 JSON 文件或 JSONiq,我将不胜感激。

【问题讨论】:

  • 为什么要写JSONiq?显然,技术的选择权在你,但看到近年来 zorba 或 JSONiq 的无进展,我认为 JSONiq 已经死了。
  • @dirkk 因为这是我的讲师分配的任务之一。它需要我们从 xquery FLWOR 表达式重写为 JSONiq。
  • JSONiq 作为一种语言是活跃的、被维护和支持的。除了 Zorba 之外,还有其他实现,例如最近的 Sparksoniq。

标签: json xquery flwor jsoniq


【解决方案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

【讨论】:

  • 感谢您的回答,尤其是解释! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-09
  • 1970-01-01
  • 2021-05-01
  • 2020-08-15
  • 2013-04-19
相关资源
最近更新 更多