Hacker News new | ask | show | jobs
by cube2222 1621 days ago
I've actually just gone ahead and added a way to do this - the zip function - in the v0.2.0 release.

The relevant jql snippet to solve this in the general case now is:

  (pipe
    (zip
      (keys)
      ((keys)))
    ((keys)
      (object
        "key" (0)
        "value" (1))))
It's not as terse as the jq equivalent - I'll probably add a way to create user-defined functions, so you can alias stuff like this to shorter forms - but that one will require more thought.
1 comments

Wasn't expecting such a quick (if any!) response! Excellent, ta. That gives me the same output from my file as jq does with `to_entries[]`.

Unfortunately my next issue is how do I iterate over an array of objects (like jq `.[]`)? I'm guessing it's maybe something to do with `range` but I don't know how many I have in order to fill in those indices and I can't do `(elem 0) ... (elem 1)` for the same reason.

Not sure if you've gone through the README - especially the first few paragraphs should help you get an intuition on how to structure nested jql queries.

Basically, you can think about the query as a composition of many functions which result in one big function taking in your JSON and outputting a new JSON.

When you do ("mykey") or (0) you dive in one level deeper. You can also transform what is that one level deeper by writing ("mykey" (mytransform)). There is a keys function which returns the list of keys or the list of indices, for the current object or list, respectively. And you can use those lists of indices for indexing purposes.

Thus, if you have an input list and want to transform it element by element, you can write ((keys) (my-single-element-transformer)). It gets the indices, uses them as an index, and transforms each object contained in the list.

So let's say you have a list of objects {"name": "abc", "surname": "xyz"} and would like to transform them into a list of {"abc": "xyz"}. You can write ((keys) (object ("name") ("surname"))). This goes over all elements and for each returns a single object with a key that is the name (it's actually a transformer/continuation which gets the name from the current object that we pass there) and value that is the surname.

You can also see that in the original "entries" query. It first zips the keys with the values, so for a list of {"mykey": "myvalue}, it will give you a list of lists ["mykey", "myvalue"]. Then it pipes that into another transform, which for each such pair creates an object {"key": "<first element of pair>", "value": "<second element of pair>"}.

The overall system isn't that straightforward at first, but playing around with it for a while should make it click and then it's easy to write even more complex queries.