Live data from Hacker News

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

github.com

31–40 of 127 posts

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

#32
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…

Would probably be shorter if you used `tostream`

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

#34
post #21

For exploring large files I released a program called "JSONSmash" that runs a shell which lets you browse the data as if it was a filesystem. https://blog.tedivm.com/open-source/2017/05/introducing-json...

What an interesting idea! On my last contract, I had to deal with 100mb+ JSON responses from a barely-documented API. This would have come in handy when I was figuring it out.

I've used JSONExplorer for this purpose, but it is web based and doesn't handle files this large.

Extending the filesystem metaphor to JSON data and re-using the same commands strikes me as a great idea.

Did another project inspire you, or did you come up with the concept yourself?

Have you done a Show HN yet?

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

#36
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…

Would probably be shorter if you used `tostream`

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

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

#38
post #7

this is not merely a display, it is really a sanitation of the brain-damaged json syntax.

What method would you use to serialize data structures into a format that is both easily read and written by both a computer and a human?

In the very rare cases that you really need to serialize complicated data structures, something like s-expressions or (gasp) json, or even a memory dump is perfectly appropriate. However, most of the times your data structures should be lists of numbers or lists of strings. For those, you do not really need a "format", you can simply print and scan them from a text file.

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

#39
post #18

Earlier quoted context omitted.

What does grep + gron give you over jq?

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.

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

#40

https://github.com/stedolan/jq/issues/243 jq can also do the same thing, with more flexibility. And it is possible to combine with bash alias to make it indistinguishable from catj

> combine with bash alias

... or you know, you could put the jq script in an executable file and add a shebang like

  #!/usr/bin/jq -jf
or

  #!/usr/bin/jq -rf
In my opinion, aliases should mostly be used to add default options only. Not really to insert whole scripts into them.
Post reply on HN