top | item 29159504

(no title)

Felk | 4 years ago

Can someone explain to me what the main differences to jq are, besides the syntax?

discuss

order

bonzini|4 years ago

jq seems to have more focus on the generator and pipe abstractions. In jq you say "foo | map(bar)"; foo and map(bar) are both generators, and bar refers to each element of foo as ".". Here you say "for $x in foo return bar"; foo and bar are both JSON objects, and bar refers to each element of foo as "$x", so the iteration is more explicit.

Likewise, compare "sum($element.response_time)" with "map(.response_time) | add" in jq. Processing in JSONiq goes inside to outside while jq goes left to right.

EdwardDiego|4 years ago

My first thought also - would be a good entry for a FAQ or blog post.

masklinn|4 years ago

Jq is xpath, this looks to be xquery. In fact it specifically works as an xquery embed.

bonzini|4 years ago

You can write the example below in jq as

    def avg: add / length;
    group_by(.url) | map({
      "url": .[0].url,
      "hits": length, 
      "avg": map(.response_time) | avg
    })
so jq should be (at least roughly) as powerful as JSONiq.

emmanueloga_|4 years ago

... although, it seems neither JSONIq nor jq contain a "parent" operator, as far as I can tell.

Deukhoofd|4 years ago

For one thing, modifying data I guess.

jolmg|4 years ago

What do you mean? jq can modify data:

  $ jq '.foo += 1' <<< '{"foo": 2}'
  {
    "foo": 3
  }

baq|4 years ago

looks nothing like jq

  1. let $stats := collection("stats")
  2. for $access in $stats
  3. group by $url := $access.url
  4. return 
  5. {
  6.   "url": $url,
  7.   "avg": avg($access.response_time),
  8.   "hits": count($access)
  9. }

keymone|4 years ago

> besides the syntax