News for hackers. Stuff that matters.
Re:Fx – Terminal JSON Viewer by tetris11 on Tuesday September 19, 2023 @09:31AM (#37569712)
Not OP but

      echo '{"name": "world"}' | fx 'x => x.name' 'x => `Hello, ${x}!`'
Is immediately grokkable compared to jq's syntax.

I love jq, I think it's extremely powerful, but I have to load up the documentation in a side window every time I have use it.

[dead]
Re:Fx – Terminal JSON Viewer by medv on Tuesday September 19, 2023 @10:05AM (#37570118)
Yes and both can be expressed in js and jq. Poinjt is what JS is known by much more peopel. As fx uses Node or Deno we for example can use lodash in fx:

    fx '_.groupBy("commit.author.name")' '_.mapValues(size)'
       '_.toPairs' '_.sortBy(1)' '_.reverse' '_.take(10)' '_.fromPairs'
To do if in jq, it will take me much more time and extensive documentation reading.
Re:Fx – Terminal JSON Viewer by gcr on Tuesday September 19, 2023 @10:27AM (#37570454)
What’s the original JSON? Happy to try and express this in jq
Re:Fx – Terminal JSON Viewer by maleldil on Tuesday September 19, 2023 @02:43PM (#37574316)
Looks like the GitHub API: https://docs.github.com/en/free-pro-team@latest/rest/commits...

I'm looking forward to your jq version. I love jq and use it all the time, but for anything of that complexity, I'd probably just use Python.

Re:Fx – Terminal JSON Viewer by wwader on Sunday September 24, 2023 @04:40AM (#37631216)
If the task was the produce an object with the top 10 commiters with username as key and value number of commits maybe something liket this:

    group_by(.commit.author.name) | map({key: first.commit.author.name, value: length}) | sort_by(.value) | reverse[:10] | from_entries
Re:Fx – Terminal JSON Viewer by lucideer on Tuesday September 19, 2023 @10:29AM (#37570483)
jq seems shorter but I don't see how it's more intuitive - ".name" is pretty basic jq syntax but how do I know \() is syntax for wrapping selectors within strings? I've used jq for many years and tbh I've never done string templating with it, so I would need to go digging in the docs.

You can argue that's the case for any language, but it's the fundamental reason DSLs rarely capture the mainstream for on-the-fly utilities. Leveraging existing knowledge of a more heavily used syntax really helps here. Look at awk for example - it's been around forever, and despite its power and ubiquity in OS installs, you'll still see sed in many many more bash scripts because it's leaning on widely used familiar syntax rather than a DSL.

That said - the fx example has "x => Hello, ${x}!" where I would expect "x => `Hello, ${x}!`", so that does indicate to me that fx has its own gotchas lurking - this example could just be contrived to look intuitive.

Re:Fx – Terminal JSON Viewer by iudqnolq on Tuesday September 19, 2023 @02:13PM (#37573903)
Maybe the person you replied to fixed a syntax error in an edit? The backticks are required, and are currently in their comment.
Re:Fx – Terminal JSON Viewer by lucideer on Wednesday September 20, 2023 @11:55AM (#37585782)
After looking more at fx that seems to be the case yes! It's 100% pure js syntax so shouldn't have any gotchas.
Re:Fx – Terminal JSON Viewer by umvi on Tuesday September 19, 2023 @12:25PM (#37572296)
jq is YADSL though vs JS which is ubiquitous. Same reason most people can't use awk or sed.
Re:Fx – Terminal JSON Viewer by Etheryte on Tuesday September 19, 2023 @12:57PM (#37572823)
As someone who doesn't use jq, a number of questions immediately jump out at me. Why is the input string double qouted ('"input"')? What does the backslash annotate? At first it looks like an escape similar to regex, but the other bracket doesn't have it so it must mean something else? Etc. Of course I could look the answers up, but the point is I can't tell without looking it up. While the code is short it feels similar to Awk, if you know it it's trivial, if you don't know it it's hard to be sure what it does without trying it out.