-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?

closed as unclear what you're asking by JigglyNaga, RalfFriedl, Michael Homer, Thomas, John WH Smith Dec 1 '18 at 13:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    jq will colourise JSON output. If you use jq with -r, you don't get JSON output and hence no colours. What does your command look like? – Kusalananda Nov 30 '18 at 13:01
  • Which version of jq are you using? JQ_COLORS is new in 1.6. – JigglyNaga Nov 30 '18 at 13:01
  • It's newest jq, 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:17
  • My bad, that's just from jq command with -r that need to preserve " so \", but without -r, it's not required. – Tuyen Pham Nov 30 '18 at 14:03
  • So, is your problem solved? If not, please clarify (by editing the question): what is the complete jq 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
1

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.

  • 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?

  • Thanks, I ended up with using sed to colorize the field instead. – Tuyen Pham Nov 30 '18 at 19:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.