News for hackers. Stuff that matters.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by jjeaff on Wednesday November 29, 2023 @03:10PM (#38464602)
Nice find. I think I'll try it out. Although I was hoping for a real SQL type experience. I don't understand why no one just copies SQL so I can write a query like "SELECT * FROM $json WHERE x>1".

Everyone seems to want to invent their own new esoteric symbolic query language as if everything they do is a game of code golf. I really wish everyone would move away from this old Unix mentality of extremely concise, yet not-self-evident syntax and do more like the power shell way.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by pgeorgi on Wednesday November 29, 2023 @05:02PM (#38465928)
> Although I was hoping for a real SQL type experience. I don't understand why no one just copies SQL so I can write a query like "SELECT * FROM $json WHERE x>1".

With somewhat tabular data, you can use sqlite to read the data into tables and then work from there.

Example 10 from https://opensource.adobe.com/Spry/samples/data_region/JSONDa... (slightly fixed by removing the ellipsis) results in this interaction:

    sqlite> select json_extract(value, '$.id'), json_extract(value, '$.type') from json_each(readfile('test.json'), '$.items.item[0].batters.batter');
    1001|Regular
    1002|Chocolate
    1003|Blueberry
    1004|Devil's Food

    sqlite> select json_extract(value, '$.id'), json_extract(value, '$.type') from json_each(readfile('test.json'), '$.items.item[0].topping');
    5001|None
    5002|Glazed
    5005|Sugar
    5007|Powdered Sugar
    5006|Chocolate with Sprinkles
    5003|Chocolate
    5004|Maple
Instead of "select" this could also flow into freshly created tables using "insert into" for more complex scenarios.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by soulbadguy on Wednesday November 29, 2023 @03:23PM (#38464743)
While i agree about the general sentiment on preferring well defined and explicit standard as opposed to "cute" custom made languages. In this case i am not convince that SQL would be the best candidate for querying nested structures like JSON.Something like xpath maybe.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by jjeaff on Wednesday November 29, 2023 @03:30PM (#38464854)
I agree, it wouldn't be the best to handle all json edge cases, but it would be a super easy way to quickly get data from a big chunk of simple json and you could just use subqueries or query chaining for nested results.

For anyone who hasn't used powershell, this is the difference I'm talking about. I would not be able to write either of these without looking up the syntax. But knowing very little about powershell, I can tell exactly what that command means while the bash command, not so much.

```powershell $json | ConvertFrom-Json | Select-Object -ExpandProperty x ```

```bash echo $json | jq '.x' ```

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by deredede on Thursday November 30, 2023 @01:55AM (#38470518)
On the other hand, I find the bash one clear and concise. That PowerShell example is so verbose, it'd drive me crazy to do any sort of complex manipulation this way! To each their own, I guess.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by jjeaff on Friday December 01, 2023 @10:31PM (#38495622)
If all I was doing is writing code, I agree. But like most developers, I think I read a lot more code than I write.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by tubthumper8 on Wednesday November 29, 2023 @10:50PM (#38469357)
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by screature2 on Wednesday November 29, 2023 @04:19PM (#38465470)
I think the closest I've seen to a SQL experience for JSON is how steampipe stores json columns as jsonb datatypes and allows you to query those columns w/postgres JSON functions etc.

- https://steampipe.io/docs/sql/querying-json#querying-json #example w/the AWS steampipe plugin (I think this is a wrapper around the AWS go SDK)

- https://hub.steampipe.io/plugins/turbot/config #I think this lets you query random json files.

(edited to try to fix the bulleting)

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by bobobar339 on Wednesday November 29, 2023 @04:37PM (#38465683)
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by pdntspa on Wednesday November 29, 2023 @04:13PM (#38465397)
Be the change you want to see.

I personally don't understand why people aren't willing to learn instead. It's not hard to sit down and pick up a new skill and it's good to step out of one's comfort zone. I personally hate Powershell syntax, brevity is the soul of wit and PS could learn a thing or two from bash and "the linux way".

We seem obsessed with molding the machine to our individual preferences. Perhaps we should obsess over the opposite: molding our mind to think more like the machine. This keeps a lot of things simple, uncomplicated, and flexible.

Does a painter wish for paints that were more like how he wanted them to be? Sure, but at the end of the day he buys the same paint everyone else does and learns to work with his medium.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by unsui on Wednesday November 29, 2023 @04:20PM (#38465476)
While I appreciate the sentiment for bending your mind, rather than the spoon, the practical reality is that developer time is far costlier than compute time.

It is easier to map compute structures and syntax to existing mental models than to formulate new mental models. The latter is effortful and time-consuming.

So, given the tradeoffs, I could learn a new language, or leverage an existing language to get things done.

And yes, given sufficient resources (particularly time), developing new mental models is ideal, but reality often prohibits the ideal.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by 3np on Wednesday November 29, 2023 @04:52PM (#38465829)
If the crux is that you want something that maps closer to your personal mental model than what's available, I guess the other option is to build the missing tool yourself. That's the other side of "be the change you want to see".

> So, given the tradeoffs, I could learn a new language, or leverage an existing language to get things done.

There is also the option to create a new language (jqsql or whatnot), optionally sharing it publically.

If you do this I think you'd find out why beyond very trivial stuff, sibling commenters have a point in that SQL isn't a good fit for nested data like JSON. Would still be a useful exercise!

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by stevage on Wednesday November 29, 2023 @04:25PM (#38465543)
In my case, my memory doesn't work that way. I have learnt jq several times but I don't use it frequently enough to retain the knowledge.

A better tool for me would be something that uses JS syntax but with some syntactic sugar and a great man page.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by throwaway2037 on Wednesday November 29, 2023 @08:34PM (#38468071)
What is "JS syntax"? And can you write a frontend for jq that converts "JS syntax" to jq syntax?

And is the jq man page poor? I'm sure they will accept patches for it.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by andelink on Wednesday November 29, 2023 @11:32PM (#38469665)
The jq man page is pretty good IMO. It’s where/how I learned to use jq
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by ruuda on Thursday November 30, 2023 @02:33AM (#38470750)
I have that same problem, the advanced features I use too little to remember. Then I started working on a configuration language that should have a non-surprising syntax (json superset, mostly inspired by Python, Rust, Nix). And it turns out, this works well as a query language for querying json documents. https://github.com/ruuda/rcl Here is an example use case: https://fosstodon.org/@ruuda/111120049523534027
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by Grimburger on Wednesday November 29, 2023 @04:46PM (#38465764)
>I personally don't understand why people aren't willing to learn instead.

Mostly because if you don't use it that often then it ends up forgotten again. I can smash out plenty of trivial regexes, but anything even slightly complicated means I'm learning backreferences again for the 6th time in a decade.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by pdimitar on Wednesday November 29, 2023 @04:57PM (#38465888)
> I personally don't understand why people aren't willing to learn instead

You misunderstand. As programmers we learn every day, obviously that's one of our strong points.

The real problem is that every single tool wants you to go deep and learn their particular dyslexic mini programming language syntax or advanced configuration options syntax. Why? We have TOML, we have SQL, we have a bunch of pretty proven syntaxes and languages that do the job very well.

A lot of these programmers authoring tools suffer from a severe protagonist syndrome which OK, it's their own personal character development to grapple with, but in the meantime us the working programmers are burning out because everyone and their dog wants us to learn their own brain child.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by imiric on Wednesday November 29, 2023 @05:11PM (#38466002)
> We seem obsessed with molding the machine to our individual preferences. Perhaps we should obsess over the opposite: molding our mind to think more like the machine.

How so? Everything in "the machine" was created by other humans; from the latest CLI tool, to the CPU instruction set. As computer users, given that it's practically impossible for a single person to be familiar with all technologies, we must pick our battles and decide which technology to learn. Some of it is outdated, frustrating to use, poorly documented or maintained, and is just a waste of time and effort to learn.

Furthermore, as IT workers, it is part of our job to choose technologies worth our and our companies' time, and our literal livelihood depends on honing this skill.

So, yes, learning new tools is great, but there's only so much time in a day, and I'd rather spend it on things that matter. Even better, if no tool does what I want it to, I have the power to create a new one that does, and increase my development skills in the process.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by vips7L on Wednesday November 29, 2023 @05:19PM (#38466085)
brevity is not clarity.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by cjaybo on Wednesday November 29, 2023 @06:27PM (#38466840)
“Brevity is the soul of wit”

Maybe we have different goals but I don’t get paid to write witty code and I don’t think anyone on my team would appreciate it if I did.

I don’t think the redeeming qualities of brevity in prose transfer to something like terse syntax.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by smabie on Wednesday November 29, 2023 @10:43PM (#38469294)
The machine is uncomplicated and simple? That is the last way I would describe modern CPUs and their peripherals.

The whole point of programming is to bend the machine towards humans, not the other way around.

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by reportgunner on Thursday November 30, 2023 @06:30AM (#38472242)
Yeah I don't understand why people aren't willing to learn SQL too.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by filmor on Wednesday November 29, 2023 @03:42PM (#38465014)
SQL is built for relational/tabular data, JSON is not relational and usually not tabular.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by im3w1l on Wednesday November 29, 2023 @04:05PM (#38465307)
Well there is nothing saying you can't put relational data in json format.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by stevage on Wednesday November 29, 2023 @04:27PM (#38465557)
But that wouldn't help query arbitrary JSON files which was the point.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by justinsaccount on Wednesday November 29, 2023 @03:38PM (#38464961)
The datafusion cli https://arrow.apache.org/datafusion/user-guide/cli.html can run SQL queries against existing json files.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by throwaway2037 on Wednesday November 29, 2023 @08:32PM (#38468051)

    do more like the power shell way
I just checked the GitHub page [1] for Microsoft PowerShell. It looks written in C# and available on Win32/MacOS/Linux, where DotNet is now supported. Do you use PowerShell only on Win32 or other platforms also?

    Everyone seems to want to invent their own new esoteric symbolic query language
Can you give an example of something that PS can do that is built-in for text processing, instead of a proprietary symbolic query language?

[1] https://github.com/PowerShell/PowerShell

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by jjeaff on Friday December 01, 2023 @10:38PM (#38495652)
By "the powershell way" I don't mean actually using powershell. I just mean using verbose, descriptive commands that one can easily understand what it does without having a working knowledge of the scripting language.
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by chthonicdaemon on Thursday November 30, 2023 @12:46AM (#38470123)
Have you looked at [duckdb's JSON support](https://duckdb.org/docs/extensions/json.html)? It's pretty transparent and you can do exactly what you say: `select * from 'file.json' where x > 1` will work with "simple" json files like {"x": 1, "y": 2} and [{"x": 1, "y":2}, {"x":2, "y":3}]
Re:Jaq – A jq clone focused on correctness, speed, and simpl... by psd1 on Thursday November 30, 2023 @07:32AM (#38472693)
nushell and pwsh. I'm not familiar with nushell, but pwsh offers where, select, foreach, group, sort.

N.B. those aliases are not created by default on *nix

It's pipeline-based and procedural, but you can be very declarative in data processing

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by nbk_2000 on Thursday November 30, 2023 @09:02AM (#38473625)
OctoSQL[1] does a pretty good job of allowing you to query JSON (and CSV) with SQL.

[1] https://github.com/cube2222/octosql

Re:Jaq – A jq clone focused on correctness, speed, and simpl... by cryptonector on Thursday November 30, 2023 @12:30PM (#38476242)
> I don't understand why no one just copies SQL so I can write a query like "SELECT * FROM $json WHERE x>1".

You could ask the same with respect to XML too -- why XPath/XSLT instead of SQL?

The problem is that SQL isn't that convenient when you're querying data in a free-form and recursive schema. Especially the latter, because recursive queries in SQL are just not pithy. I say this as someone who loves SQL.