HN user

wwader

298 karma
Posts2
Comments102
View on HN

Assume input is an array you can do it several ways depending on what you want:

    # output each value in the array separately 
    .[]

    # output each value but transform it in some way
    .[] | . * 2


    # map each value into a new array
    map(. * 2) 

    # same as above but manual iterate/collect
    [.[] | . * 2]

Same! and jq's regexp functions are quite powerful for transforming text into something more structured:

  $ echo -e "10:20: hello\n20:39: world" | jq -cR 'capture("^(?<timestamp>.*): (?<message>.*)$")'
  {"timestamp":"10:20","message":"hello"}
  {"timestamp":"20:39","message":"world"}


  $ echo -e "10:20: hello\n20:39: world" | jq -Rn '[inputs | capture("(?<t>.*): (?<m>.*)$").m] | join(" ")'
  "hello world"
Also using inputs/0 etc in combination with reduce and foreach it's possible to process streams of text that might not end.

Yeap the syntax and semantics is quite different to other languages and it really took me a long time and deep understanding to really appreciate how expressive and damn well designed it is.

Oh oh, jq has bugs..., I hope you reported them. I wondered more than once whether I could trust the results of my jq invocations.

Yes report, and i'm one of the jq maintainers now a days :) and i think you can trust the result if your doing common things. The bugs i've encountered have usually been in exotic parts, ex https://github.com/jqlang/jq/issues/3128

Yes it is! see the example in the readme how to run jqjq with jqjq. Sadly you will probably need a lot of memory, it used to work on my laptop but not anymore after some recent changes that seem to have increased memory use.

Hey! quite accurate, it was a mix of i just wanted to try to see how hard it would be and wanted to know more about jq. I guess it has limited practical use other than as a learning tool. But i've used it to prototype features for "original" jq like how eval could work, regex literals, some object literal syntax enhancements etc. Also i found a bunch of bugs and confusing behaviours in jq along the way :)

btw i didn't follow what you mean by "overlooked the 2 characters"?

Hey! jqjq author here. Some time :) but surpassingly not that much, after getting over some major hurdles like how to mix recursive decent parsing and infix operators (precedence climbing is nice) and how to eval jq's call semantic that uses thunk/lambdas args it was mostly to grind through the rest. Actually string interpolation was quite tricky also. Also helped that i've worked on various jq other implementations and also written quite a lot of jq in general :)

Thanks for the kind words! I have to pass along most of the credit to the designers of jq and jq CLI interface. Lots of care and thought have gone into those, also gojq was big enabler. I feel more like plumber that half accidentally connected a bitstream decoder with a hacked up version of jq :D

Note that gojq also fixes and improves on some things compared to jq, like arbitrary-precision arithmetic (jq only preserves), some semantic and parsing improvement and fixes that will likely happen in jq at some point also, time zone/formatting/parsing fixes, etc

jq tries to preserve number precision if you don't do operation with them, but as you noted this is within some sanity. If you do operations the involved numbers will first be converted to binary64 (aka double), same as node and most other languages. This is what is recommended by RFC 7159 for interoperability.