Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

Make jq output an escaped sequence [duplicate]

$
0
0

I can't seem to get jq to escape a TAB character for me properly. This is a json I'm working with:

{"api01": {"before": {"path": "api01","user": "admin"    }  },"server01": {"before": {"path": "server01","user": "admin"    }  },"server11": {"before": {"path": "server11","user": "admin"    }  },"staging01": {"after": {"path": "/home/admin/test/user/dotfiles","user": "git"    },"before": {"path": "staging01","user": "admin"    }  }}

Now I tried to iterate over every json key and value in bash and output the corresponding string:

jq 'to_entries[] | "\(.key)\t\(.value)"' test.json | while IFS=$'\t' read -r key value; do  echo "key = $key, value = $value"done

But the output is not split into key and value pairs:

key = "api01\t{\"before\":{\"path\":\"api01\",\"user\":\"admin\"}}", value =key = "server01\t{\"before\":{\"path\":\"server01\",\"user\":\"admin\"}}", value =key = "server11\t{\"before\":{\"path\":\"server11\",\"user\":\"admin\"}}", value =key = "staging01\t{\"after\":{\"path\":\"/home/admin/test/user/dotfiles\",\"user\":\"git\"},\"before\":{\"path\":\"staging01\",\"user\":\"admin\"}}", value =

As you can see value = , so value remains unassigned.

The problem seems to be that jq inserts the \t character literally, as can be seen in this hexdump:

jq 'to_entries[] | "\(.key)\t\(.value)"' test.json | hexdump -C00000000  22 61 70 69 30 31 5c 74  7b 5c 22 62 65 66 6f 72  |"api01\t{\"befor|00000010  65 5c 22 3a 7b 5c 22 70  61 74 68 5c 22 3a 5c 22  |e\":{\"path\":\"|00000020  61 70 69 30 31 5c 22 2c  5c 22 75 73 65 72 5c 22  |api01\",\"user\"|00000030  3a 5c 22 61 64 6d 69 6e  5c 22 7d 7d 22 0a 22 73  |:\"admin\"}}"."s|00000040  65 72 76 65 72 30 31 5c  74 7b 5c 22 62 65 66 6f  |erver01\t{\"befo|00000050  72 65 5c 22 3a 7b 5c 22  70 61 74 68 5c 22 3a 5c  |re\":{\"path\":\|00000060  22 73 65 72 76 65 72 30  31 5c 22 2c 5c 22 75 73  |"server01\",\"us|00000070  65 72 5c 22 3a 5c 22 61  64 6d 69 6e 5c 22 7d 7d  |er\":\"admin\"}}|00000080  22 0a 22 73 65 72 76 65  72 31 31 5c 74 7b 5c 22  |"."server11\t{\"|00000090  62 65 66 6f 72 65 5c 22  3a 7b 5c 22 70 61 74 68  |before\":{\"path|000000a0  5c 22 3a 5c 22 73 65 72  76 65 72 31 31 5c 22 2c  |\":\"server11\",|000000b0  5c 22 75 73 65 72 5c 22  3a 5c 22 61 64 6d 69 6e  |\"user\":\"admin|000000c0  5c 22 7d 7d 22 0a 22 73  74 61 67 69 6e 67 30 31  |\"}}"."staging01|000000d0  5c 74 7b 5c 22 61 66 74  65 72 5c 22 3a 7b 5c 22  |\t{\"after\":{\"|000000e0  70 61 74 68 5c 22 3a 5c  22 2f 68 6f 6d 65 2f 61  |path\":\"/home/a|000000f0  64 6d 69 6e 2f 74 65 73  74 2f 75 73 65 72 2f 64  |dmin/test/user/d|00000100  6f 74 66 69 6c 65 73 5c  22 2c 5c 22 75 73 65 72  |otfiles\",\"user|00000110  5c 22 3a 5c 22 67 69 74  5c 22 7d 2c 5c 22 62 65  |\":\"git\"},\"be|00000120  66 6f 72 65 5c 22 3a 7b  5c 22 70 61 74 68 5c 22  |fore\":{\"path\"|00000130  3a 5c 22 73 74 61 67 69  6e 67 30 31 5c 22 2c 5c  |:\"staging01\",\|00000140  22 75 73 65 72 5c 22 3a  5c 22 61 64 6d 69 6e 5c  |"user\":\"admin\|00000150  22 7d 7d 22 0a                                    |"}}".|00000155

Where literal \t corresponds to (5c 74).

So how do I advise jq to output an actual tab character so that it can be split correctly by my bash while read?


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>