본문 바로가기

리눅스

[리눅스] jq 명령어

반응형

jq 명령어

jq 설치

yum install -y epel-release
yum install -y jq

jq 버전

jq --version
$ jq --version
jq-1.6

jq를 사용하여 json을 한 줄로 변환하는 방법

file.txt 파일

$ cat file.txt
{"ID":"web","Service":"web","Tags":["django"],"Meta":{},"Port":80,"Address":"","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false,"ContentHash":"318c41a7ce882f20","Datacenter":"vm-dc1"}

cat file.txt | jq .

cat file.txt | jq .
{
  "ID": "web",
  "Service": "web",
  "Tags": [
    "django"
  ],
  "Meta": {},
  "Port": 80,
  "Address": "",
  "Weights": {
    "Passing": 1,
    "Warning": 1
  },
  "EnableTagOverride": false,
  "ContentHash": "318c41a7ce882f20",
  "Datacenter": "vm-dc1"
}

--

cat file.txt | jq . > file.json
$ cat file.json
{
  "ID": "web",
  "Service": "web",
  "Tags": [
    "django"
  ],
  "Meta": {},
  "Port": 80,
  "Address": "",
  "Weights": {
    "Passing": 1,
    "Warning": 1
  },
  "EnableTagOverride": false,
  "ContentHash": "318c41a7ce882f20",
  "Datacenter": "vm-dc1"
}

cat file.txt | jq -c

cat file.txt | jq -c
{"ID":"web","Service":"web","Tags":["django"],"Meta":{},"Port":80,"Address":"","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false,"ContentHash":"318c41a7ce882f20","Datacenter":"vm-dc1"}
cat file.txt | jq -c > file.jsonl
$ cat file.jsonl
{"ID":"web","Service":"web","Tags":["django"],"Meta":{},"Port":80,"Address":"","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false,"ContentHash":"318c41a7ce882f20","Datacenter":"vm-dc1"}
반응형