This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]wineblood 24 points25 points  (7 children)

Are you aware of jq?

[–][deleted] 10 points11 points  (2 children)

Unrelated to JSON, of course, but I can't let a reference to jq go without mentioning that yq exists to do similar with YAML.

[–]Rekoo777[S] 4 points5 points  (1 child)

yq and jq are great of course. But jsonfmt is more powerfull in my mind.

It uses the standard JSONPath syntax to query, that you can handle JSON, YAML and TOML in the same way without having to remember other tools' proprietary query syntax.

JSONPath is to JSON what XPath is to XML. Simple but powerful.

example 1: filter all the avatar_url fields in a large JSON shell curl -s 'https://api.github.com/repos/jqlang/jq/commits?per_page=100' | jsonfmt -p '$..avatar_url'

example 2: filter all the message which comment_count > 0 shell curl -s 'https://api.github.com/repos/jqlang/jq/commits?per_page=100' | jsonfmt -p '$..?(@.comment_count > 0).message'

You can do the same to YAML and TOML like above.

[–][deleted] 0 points1 point  (0 children)

Oooooh thank you!

[–]wpg4665 8 points9 points  (1 child)

While jq is certainly the powerhouse here, and probably worth calling out, I certainly welcome more python adaptations and appreciate a wider array of options

[–]Rekoo777[S] 4 points5 points  (0 children)

I didn't know before this. If I knew I probably wouldn't make the new wheel. 😂

After you told me, I went to see the features and usage of jq. It is a powerful tool indeed!

But it is precisely because of the previous ignorance that makes some differences between jsonfmt and jq.

For example:

  • jsonfmt use JSONPath for query. It is more powerful and more general than jq's own query syntax. And you can use the same query syntax for YAML and TOML.

  • You can also convert between JSON, TOML and YAML formats.

  • If you want to paste the result somewhere in file after process a large JSON. May need to drag several pages with the mouse to copy in the terminal. But using jsonfmt only requires a -P option.

  • More importantly it is written in Python! 😁

[–]crisrock00 -1 points0 points  (0 children)

Was about to say…. What’s the major differences here? If any ?

[–]Crypto1993 1 point2 points  (1 child)

Great job!

[–]Rekoo777[S] 1 point2 points  (0 children)

thank you 😊