-1

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?

Tuyen Pham
  • 1,805

2 Answers2

2

I'm still not sure what exactly the problem is. If this is about how jq colorizes the output, have a look at man jq:

   o   --color-output / -C and --monochrome-output / -M:

       By default, jq outputs colored JSON if writing to a terminal. You can force it to produce color  even  if
       writing to a pipe or a file using -C, and disable color with -M.

So when writing to a pipe, you are not writing to a terminal, which is why there are no colors. Test it by comparing

echo '{"foo":"bar"}' | jq .
echo '{"foo":"bar"}' | jq . | cat

And you can turn on coloring again by adding -C:

echo '{"foo":"bar"}' | jq -C . | cat

If you want to do something more difficult, like imitating the way jq colors JSON values, but you don't want it for JSON field names, or just for some parts of the object, please update your question.

Note that coloring just consists of adding escape codes to the output. You can imitate this in various ways.

dirkt
  • 32,309
  • Thanks, almost solve all my questions. But with -C, I got in situation that \t can't be expanded: "something here has spaces and inside a double quotes"\tsolid_line_without_space – Tuyen Pham Nov 30 '18 at 15:46
  • Also, because two field has same color with default of jq, how to inline imitate the second field, like to white color one? – Tuyen Pham Nov 30 '18 at 15:51
1

From the jq documentation:

To configure alternative colors just set the JQ_COLORS environment variable to colon-delimited list of partial terminal escape sequences like "1;31", in this order:

  • color for null
  • color for false
  • color for true
  • color for numbers
  • color for strings
  • color for arrays
  • color for objects

Those are the only components you can change, and you get exactly one setting per entry in the list. You can't pick and choose individual members with JQ_COLORS, so all strings will be handled the same.

As your output is no longer in JSON, there's no requirement to keep handling it in jq. You could pass it to another command to add the correct terminal escape sequences. See eg. "Friendly" terminal color names in shell scripts?

JigglyNaga
  • 7,886