Live data from Hacker News

Show HN: Catj – A new way to display JSON files

github.com

71–80 of 127 posts

Re: Show HN: Catj – A new way to display JSON files

#71

You can do this easily with the json_tree function from SQLite's JSON1 extension. It's given as an example in the documentation: https://sqlite.org/json1.html#jtree SELECT big.rowid, fullkey, value FROM big, json_tree(big.json) WHERE json_tree.type NOT IN ('object','array');

And with the fileio sqlite extension, you can even directly query files (and their contents, and directories recursively too, no less) from SQL.

Re: Show HN: Catj – A new way to display JSON files

#72
post #36

Earlier quoted context omitted.

You're right. Good tip: jq -r ' tostream | select(length > 1) | ( .[0] | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ) + " = " + (.[1] | @json) ' EDIT: For those who want to save this script, you can put just the jq code in an executable file with the shebang: #!/usr/bin/jq -rf

Appending | to each line and a last line . is a jq program that reproduces the original json. jq -r ' ( tostream | select(length > 1) | ( .[0] | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ) + " = " + (.[1] | @json) + " |" ), "." '

That's insane. Here I was thinking about how much more challenging it would be to parse and reconstruct the object from jq, and you got the idea to take advantage of the syntax similarity to parse it as jq code itself. Nice. And so, that means the inverse of the jq code I posted would simply be:

  ( jq "$(sed 's/$/ |/;$a.')" 
As in:

  catj example.json \
  | ( jq "$(sed 's/$/ |/;$a.')"  original.json

Re: Show HN: Catj – A new way to display JSON files

#73
post #26

Earlier quoted context omitted.

json will be mocked in a few years by the same people that today mock xml. In the end, unix always survives.

"unix" isn't a file format, nor is it in competition with either XML or JSON, so claiming it will "survive" either is a meaningless boast.

While I don’t disagree with you, if you look at the comment above this you’ll see @enriquto mentions just dumping lists to a text file. Pretty sure that’s what was implied by ‘unix’: use a simple text file that is dumped to the fs that can be easily parsed by standard tools on any Unix-like.

Re: Show HN: Catj – A new way to display JSON files

#74
post #39
post #18

Earlier quoted context omitted.

jq seems very powerful. I don't deal with json all that often and my most common use case (by far) is `jq '.' -C` and it took a few tries for me to remember that syntax. The idea of flattening, grepping, then reverting sounds very appealing and sounds like a better fit for me.

> `jq '.' -C` and it took a few tries for me to remember that syntax. I don't think you really need neither `.` nor `-C`. Just `jq` seems to do the same colored output of the input by default.

It does look like neither are needed if you pipe a file in jq, but `jq . file.json` requires the `.` and if you're pipeing into a pager, like less, you need both `.` and `-C` to get colored output (that was the case with the alias I had pulled up). I am using 1.5 and haven't looked to see if 1.6 changes this.

Re: Show HN: Catj – A new way to display JSON files

#75
post #74
post #39

Earlier quoted context omitted.

> `jq '.' -C` and it took a few tries for me to remember that syntax. I don't think you really need neither `.` nor `-C`. Just `jq` seems to do the same colored output of the input by default.

It does look like neither are needed if you pipe a file in jq, but `jq . file.json` requires the `.` and if you're pipeing into a pager, like less, you need both `.` and `-C` to get colored output (that was the case with the alias I had pulled up). I am using 1.5 and haven't looked to see if 1.6 changes this.

I see. I doubt that behavior has changed, then.

`-C` would be required when piping because most of the time (with the exception of piping into less) when stdout is not a terminal, it doesn't make sense to include terminal color escape sequences. You'd end up with those codes in your files, and grep would be looking at them for matches, for example.

`.` would be required when passing the file as an argument instead of stdin, because jq interprets the first argument as jq-code. If you don't include `.` it would interpret the filename as jq-code.

Re: Show HN: Catj – A new way to display JSON files

#76
post #5

Have you seen gron[0]? It's similar: flattens JSON documents to make them easily greppable. But it also can revert (ie, ungron) so you can pipe json to gron, grep -v to remove some data, then ungron to get it back to json. [0] https://github.com/tomnomnom/gron

What does grep + gron give you over jq?

I find it useful when I don't know what the json schema is. Then you can just do a quick gron + grep and find where the interesting parts of a large json document are.

Re: Show HN: Catj – A new way to display JSON files

#78
post #36

Earlier quoted context omitted.

You're right. Good tip: jq -r ' tostream | select(length > 1) | ( .[0] | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ) + " = " + (.[1] | @json) ' EDIT: For those who want to save this script, you can put just the jq code in an executable file with the shebang: #!/usr/bin/jq -rf

Appending | to each line and a last line . is a jq program that reproduces the original json. jq -r ' ( tostream | select(length > 1) | ( .[0] | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ) + " = " + (.[1] | @json) + " |" ), "." '

[deleted]

Re: Show HN: Catj – A new way to display JSON files

#79
post #20

I was curious if this could be doable with jq, and apparently it is: jq -j ' [ [ paths(scalars) | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ], [ .. | select(scalars) | @json ] ] | transpose | map(join(" = ") + "\n") | join("") ' EDIT: Got the string quoting and escaping. EDIT 2: For those who want to save this script, you can put just the jq code in an executable file with the s…

That's awesome work. The only problem is that it does not properly handle keys which are not valid JS identifiers (like 1foo, @foo, foo-bar, etc.).

Re: Show HN: Catj – A new way to display JSON files

#80
post #9

That's nice, I like that. Related, if you want more of a csv-style, see JSONLines. aka "newline-delimited JSON" http://jsonlines.org/

I don't see what jsonlines has over yaml; in fact the jsonlines examples presented there are almost trivially converted to yaml. e.g. the first example is valid yaml if you add a '- ' to each line. And JSON is (almost) a perfect subset of yaml. I've been using csv lately. It's reputation is overstated. What I like is that it's far more compact than yaml or json and trivially pulled into sqlite for ad-hoc queries.

> ... and trivially pulled into sqlite for ad-hoc queries.

Or spreadsheets, to work out a plan, and then MySQL.

Post reply on HN