Extend of this question: jq: parse json file with constraint from other field
After select "name"
field I'd like to colorize it:
{
"checksum": "9e44bb7b76d8c39c45420dd2158a4521",
"roots": {
"bookmark_bar": {
"children": [ {
"children": [ {
"date_added": "13161269379464568",
"id": "2046",
"name": "The title is here",
"sync_transaction_version": "1",
"type": "url",
"url": "https://the_url_is_here"
}, {
After google for a while and read through jq
's manpage, seem we can set color via variable called JQ_COLORS
with default JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39
, I don't have it in my env so I manually set on (I shouldn't because it's default - hardcoded somewhere), and test against jq
command, but the output has no color (output is just a field, not an json object). I so guess the color through jq
is for json object, not the selected field.
So I'm asking is there a way to set color for the selected field with jq
?
EDIT: I'm stuck with -r
or not -r
option to jq
:
With this command:
jq -r '.roots.bookmark_bar.children[]|.children[]|["\"\(.name)\"",.url]|@tsv' json_file`
I have expected result of:
"something here has spaces and inside a double quotes" solid_line_without_space
But If I leave -r
option, I have command:
jq '.roots.bookmark_bar.children[]|.children[]|["\(.name)",.url]|@tsv' json_file`
and the result like below - not expected, \t
can't be expanded:
"something here has spaces and inside a double quotes"\tsolid_line_without_space
Question 1: how to achieve result:
"something here has spaces and inside a double quotes" solid_line_without_space
without -r
option, I really need color on here.
Question 2: Because I filter two fields in my query, how to keep color on only field "name"
and not on field "url"
- in fact it can be extended to how to customize color of each field (guess I have to modify JQ_COLORS
)
Question 3: extended question: how to keep color of fields if I pipe it through another filter, e.g: jq <..> | sed <...>
?
Question 4: How to have a customized separator? here I had @tsv
as sym for <tab>
, what if about a separator like |
- space|space
?
jq
will colourise JSON output. If you usejq
with-r
, you don't get JSON output and hence no colours. What does your command look like? – Kusalananda Nov 30 '18 at 13:01jq
are you using?JQ_COLORS
is new in 1.6. – JigglyNaga Nov 30 '18 at 13:01jq
, ver 1.6. If I don't give-r
option then the output is"\"A<..>
that add extra\"
. With-r
I got what is expected"A<..>
. So-r
brings more than just color/not. – Tuyen Pham Nov 30 '18 at 13:17jq
command with-r
that need to preserve"
so\"
, but without-r
, it's not required. – Tuyen Pham Nov 30 '18 at 14:03jq
command you ran, what is the full output, and which bit(s) of it do you want to appear differently? – JigglyNaga Nov 30 '18 at 15:05