在JSONiq中使用键值对将对象存储在变量中。

huangapple go评论48阅读模式
英文:

Storing an object with key value pairs in a variable in JSONiq

问题

这是一个示例的JSON数组:

"transcript": [
    {
        "dcode": "CS",
        "cno": 211,
        "ssn": 82,
        "grade": "A"
    },
    {
        "dcode": "CS",
        "cno": 211,
        "ssn": 75,
        "grade": "A"
    },
    {
        "dcode": "MTH",
        "cno": 125,
        "ssn": 82,
        "grade": "F"
    }
]

我需要创建一个名为 $courses 的变量,其中包含所有由具有 ssn 为 82 的学生所选的课程。我应该如何遍历该数组并创建这个变量?

我希望输出的结果是 $courses:= {"CS": 211, "MTH": 125}

我尝试在 {} 括号中编写循环,但未成功。我是 JSONiq 新手。

英文:

This is a sample JSON array

"transcript":[
       {
         "dcode": "CS",
         "cno": 211,
         "ssn": 82,
         "grade": "A"
       },
       {
         "dcode": "CS",
         "cno": 211,
         "ssn": 75,
         "grade": "A"
       },
       {
         "dcode": "MTH",
         "cno": 125,
         "ssn": 82,
         "grade": "F"
       }
 
 ]

I need to create a variable $courses which gives me all the courses taken by a student whose ssn is 82. How do I loop over this and create this variable?

I want the output to be $courses:= {"CS" : 211, "MTH" : 125}

I tried to write the loop in {} brackets but it did not work. I am new to Jsoniq

答案1

得分: 0

以下是翻译好的部分:

{
  "transcript": [
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 82,
      "grade": "A"
    },
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 75,
      "grade": "A"
    },
    {
      "dcode": "MTH",
      "cno": 125,
      "ssn": 82,
      "grade": "F"
    }
  ]
}

假设此对象存储在变量 `$x` 中,您可以使用以下方式获取所需的对象:

$x.transcript[][$$.ssn eq 82]

其中 `[]` 将数组解包为对象序列,而 `[$$.ssn eq 82]` 过滤此序列,仅保留其中 ssn  82 的对象。

因此,完整的查询如下:

let $x := {
  "transcript": [
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 82,
      "grade": "A"
    },
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 75,
      "grade": "A"
    },
    {
      "dcode": "MTH",
      "cno": 125,
      "ssn": 82,
      "grade": "F"
    }
  ]
}
return $x.transcript[][$$.ssn eq 82]

也可以使用显式的 FLWOR 子句和 where 子句的替代方式(可以根据更复杂的用例自由扩展):

let $x := {
  "transcript": [
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 82,
      "grade": "A"
    },
    {
      "dcode": "CS",
      "cno": 211,
      "ssn": 75,
      "grade": "A"
    },
    {
      "dcode": "MTH",
      "cno": 125,
      "ssn": 82,
      "grade": "F"
    }
  ]
}
for $course in $x.transcript[]
where $course.ssn eq 82
return $course

现在,我们可以使用 let 子句将这些结果绑定到变量 `$courses` 中,请注意这是一个函数式语言,这些是绑定而不是赋值。在这种简单情况下,return 子句返回了 `$courses` 的内容,但也可以对此变量执行其他操作。
英文:

First a quick comment: you need curly braces to make sure this is well-formed (JSON and JSONiq):

{
"transcript": [
{
"dcode": "CS",
"cno": 211,
"ssn": 82,
"grade": "A"
},
{
"dcode": "CS",
"cno": 211,
"ssn": 75,
"grade": "A"
},
{
"dcode": "MTH",
"cno": 125,
"ssn": 82,
"grade": "F"
}
]
}

Assuming this object is stored in a variable $x, you can obtain the desired object with

$x.transcript[][$$.ssn eq 82]

where [] unboxes the array into a sequence of objects, and [$$.ssn eq 82] filters this sequence keeping only those where ssn is 82.

So a complete query would be:

let $x := {
"transcript":[
{
"dcode": "CS",
"cno": 211,
"ssn": 82,
"grade": "A"
},
{
"dcode": "CS",
"cno": 211,
"ssn": 75,
"grade": "A"
},
{
"dcode": "MTH",
"cno": 125,
"ssn": 82,
"grade": "F"
}
]
}
return $x.transcript[][$$.ssn eq 82]

There is also the alternative with an explicit FLWOR for clause and where clause (which are extensible at will with more complex use cases):

  let $x := {
"transcript":[
{
"dcode": "CS",
"cno": 211,
"ssn": 82,
"grade": "A"
},
{
"dcode": "CS",
"cno": 211,
"ssn": 75,
"grade": "A"
},
{
"dcode": "MTH",
"cno": 125,
"ssn": 82,
"grade": "F"
}
]
}
for $course in $x.transcript[]
where $course.ssn eq 82
return $course

Now, we can bind these results to a variable $courses with a let clause -- it is important to understand that this is a functional language, that is, these are bindings and not assignments. In this simple case, the return clause returns the contents of $courses, but it is also possible to do anything else you want with this variable instead.

let $courses :=
let $x := {
"transcript":[
{
"dcode": "CS",
"cno": 211,
"ssn": 82,
"grade": "A"
},
{
"dcode": "CS",
"cno": 211,
"ssn": 75,
"grade": "A"
},
{
"dcode": "MTH",
"cno": 125,
"ssn": 82,
"grade": "F"
}
]
}
for $course in $x.transcript[]
where $course.ssn eq 82
return $course
return $courses

huangapple
  • 本文由 发表于 2023年2月24日 15:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定