Skip to content
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

Create jqfmt to format jq scripts #2366

Open
colindean opened this issue Nov 4, 2021 · 5 comments
Open

Create jqfmt to format jq scripts #2366

colindean opened this issue Nov 4, 2021 · 5 comments

Comments

@colindean
Copy link

colindean commented Nov 4, 2021

I've written an appreciably long jq template that I'm rationally storing in a file in invoking with jq --from-file script.jq. Frustratingly, I had to format it myself by hand in order to understand and edit it better. It'd be convenient if there was a jqfmt binary or jq fmt command built-in that could take a template and parse it, then output it in the standard formatting, kinda like gofmt, scalafmt, rustfmt, Black for Python, etc.

Example, about 13 lines from my long script bordering on "I probably should have done this in $language instead of jq":

[[ .[] | select( .algo_name | [.] | inside($algo_names | split(",")) ) ] | group_by(.strategy) | .[] | sort_by(.algo_timestamp | fromdateiso8601) | last ]

would become something like

[
    [
      .[]
      | select(
        .algo_name
        | [.]
        | inside($algo_names | split(","))
      )
    ]
    | group_by(.strategy)
    | .[]
    | sort_by(.algo_timestamp | fromdateiso8601)
    | last
  ]

with a "pretty print" option or remain like it was (manually adjusted from the latter in this example) in a "compressed" one-liner.

@colindean
Copy link
Author

This could also morph into a bit of a linter/best practice recommender, as reading through builtin.jq has taught me that I'm not using the built-ins very efficiently in my long template!

@xguo-prestolabs
Copy link

jq program is usually very short, I mean 100 lines of jq program is like very big. Manually formatting isn't a big deal, I guess that's why nobody is working on it.

I remember there is vscode plugin that can format jq program, maybe you should try it.

@noperator
Copy link

Hi, @colindean. I took a stab at jqfmt here: https://github.com/noperator/jqfmt. Let me know what you think!

@SOF3
Copy link
Contributor

SOF3 commented Jul 4, 2024

Are there even any formatting guidelines written in prose anywhere? For example, it seems everyone has different opinions on whether | should be end-of-line or start-of-next-line.

@wader
Copy link
Member

wader commented Jul 4, 2024

Not that i know of. I personally prefer and have setteled on a quite verbose "haskell"-style for bigger jq programs, see https://github.com/wader/jqjq for example how it looks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants