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');
Show HN: Catj – A new way to display JSON files
71–80 of 127 posts
Re: Show HN: Catj – A new way to display JSON files
#72Earlier 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) + " |" ), "." '
( jq "$(sed 's/$/ |/;$a.')"
As in: catj example.json \
| ( jq "$(sed 's/$/ |/;$a.')" original.jsonRe: Show HN: Catj – A new way to display JSON files
#73Earlier 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.
Re: Show HN: Catj – A new way to display JSON files
#74Earlier 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.
Re: Show HN: Catj – A new way to display JSON files
#75Earlier 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.
`-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
#76Have 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?
Re: Show HN: Catj – A new way to display JSON files
#77Re: Show HN: Catj – A new way to display JSON files
#78Earlier 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) + " |" ), "." '
Re: Show HN: Catj – A new way to display JSON files
#79I 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…
Re: Show HN: Catj – A new way to display JSON files
#80That'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.
Or spreadsheets, to work out a plan, and then MySQL.