-
Notifications
You must be signed in to change notification settings - Fork 83
Paths() Function Support #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @osevill, you should be able to test your filter with jaq using two changes:
Let us know how your performance (and your final filter) looks like! Regarding jaq-specific documentation: I've submitted a PR to the jq repository that adds this information to the jq manual itself, showing for example that |
(Small improvement suggestion: |
Thank you for the suggestions. Will implement and let you know. |
Hi @osevill, just wanted to let you know that your use case motivated me to implement support for |
tyvm.
...will I get the path-values branch, or do I need to specify the branch above? Also, what's the prefered method for providing you sample json input data? |
should do it.
Just posting one or two lines of JSON in this thread, for example. :) The best thing would be to have a small jq script that produces arbitrarily large input data. |
Successfully compiled the path-values branch and did some testing with the filter in my original post:
The updated jaq seems to work fine with this filter when keys are the same between elements of the json array. When keys from element to element differ however, I get the following error: Here's a sample schema typical of what I use with jq to convert to csv. There are 3 elements in the array. The first includes inner_array_1, the second includes inner_array_2, and the third element includes both. (For my use case with the above jq filter, it's appropriate to consider the nested array keys as part of the respective outer array element, i.e., no matter how many elements are in the inner arrays, there should only be 3 csv rows because the nested array elements don't define new records. Only the outer array elements define new records in this use case.) I also added an additional scalar "field6" key to the third outer array element.
Even when I simplify the sample json to just 2 records, with 2 keys each (one scalar that matches between the records; one object that differs, I also get the same "cannot use null as iterable" error.
...but if both keys of the record are scalar and the second scalar key differs between records, the path-values jaq branch successfully converts to csv:
Regards |
The "cannot use null as iterable" error is to be expected, because of the way I implemented (.[] | map(paths(scalars|true)) | unique) as $cols |
map(.[] as $row | ($cols | map(. as $col | $row | getpath($col)? // null))) as $rows |
([($cols | map(. | map(tostring) | join(".")))] + $rows) | map(@csv) | .[] |
I made a little change to implement
I generated |
Thank you again for your time working on this request. I'll recompile the path-values branch to get the updated getpath, and try it out again. Should have some time toward the end of the week. |
I currently use jq to convert a json object to csv format, generically without referring to field names (usually a json object with an array). The filter looks like this:
jq -r '(.[] | map(paths(scalars|true)) | unique) as $cols | map(.[] as $row | ($cols | map(. as $col | $row | getpath($col)))) as $rows | ([($cols | map(. | map(tostring) | join(".")))] + $rows) | map(@csv) | .[]'
I would like to see if there's any performance gain using jaq for the above task, but when I get started, trying
paths(scalars)
, I get an "undefined filter" error.Since the function
paths()
doesn't seem to be supported, is there an alternate way to get paths to scalar values in jaq?Also, is there jaq-specific documentation that lists available operators and functions?
Thanks!
The text was updated successfully, but these errors were encountered: