linux에서 json Pretty Print 하기 (jq 명령어)

목적

curl 을 이용해서 얻어온 json 포멧을 

terminal에서 보기 좋게 만들자

curl http://localhost:8080/cars
{"_embedded":{"carList":[{"id":1,"createdAt":"2022-08-11T22:59:40.56995","modifiedAt":"2022-08-11T22:59:40.56995","condition":"USED","details":{"body":"wood","model":"crow","manufacturer":{"code":100,"name":"Audi"},"numberOfDoors":null,"fuelType":null,"engine":null,"mileage":null,"modelYear":null,"productionYear":null,"externalColor":null},"location":{"lat":0.0,"lon":0.0,"address":null,"city":null,"state":null,"zip":null},"price":null,"_links":{"self":{"href":"http://localhost:8080/cars/1"},"cars":{"href":"http://localhost:8080/cars"}}},{"id":2,"createdAt":"2022-08-11T22:59:40.577338","modifiedAt":"2022-08-11T22:59:40.577338","condition":"USED","details":{"body":"steel","model":"house","manufacturer":{"code":100,"name":"Audi"},"numberOfDoors":null,"fuelType":null,"engine":null,"mileage":null,"modelYear":null,"productionYear":null,"externalColor":null},"location":{"lat":0.0,"lon":0.0,"address":null,"city":null,"state":null,"zip":null},"price":null,"_links":{"self":{"href":"http://localhost:8080/cars/2"},"cars":{"href":"http://localhost:8080/cars"}}}]},"_links":{"self":{"href":"http://localhost:8080/cars"}}}

위와 같이 리눅스 terminal에서는 json 포멧을 예쁘게 보기 어렵다.

(아.. json을 더욱 길고 어렵게 만드는 hateoas 이름 만큼이나 밉다)

jq설치

$ sudo apt install jq
[sudo] steven 암호: 
죄송합니다만, 다시 시도하십시오.
[sudo] steven 암호: 
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
패키지 jq는 이미 최신 버전입니다 (1.6-1ubuntu0.20.04.1).
다음 패키지가 자동으로 설치되었지만 더 이상 필요하지 않습니다:
  libfwupdplugin1
'sudo apt autoremove'를 이용하여 제거하십시오.
0개 업그레이드, 0개 새로 설치, 0개 제거 및 27개 업그레이드 안 함.

sudo apt install jq 를 해서 jq를 install 해준다.

사용 방법은 아래처럼 하면 된다.

json 파일 print

jq . <<파일명>>

$ jq . a.json
{
  "_embedded": {
    "carList": [
      {
        "id": 1,
        "createdAt": null,
        "modifiedAt": null,
        "condition": "USED",
        "details": {
          "body": "Body",
          "model": "Audi",
          "manufacturer": {
            "code": 100,
            "name": "Audi"
          },
          "numberOfDoors": null,
          "fuelType": null,
          "engine": null,
          "mileage": null,
          "modelYear": null,
          "productionYear": null,
          "externalColor": null
        },
        "location": null,
        "price": null,
        "_links": {
          "self": {
            "href": "http://localhost:8080/cars/1"
          },
          "cars": {
            "href": "http://localhost:8080/cars"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/cars"
    }
  }
}

 

파이프라인 입력

<<json 나오는 명령어>> | jq .

$ echo '{"foo": 0}' | jq .
{
  "foo": 0
}

 

<<curl url>> | jq .

$ curl http://localhost:8080/cars | jq -C .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1123    0  1123    0     0   109k      0 --:--:-- --:--:-- --:--:--  109k
{
  "_embedded": {
    "carList": [
      {
        "id": 1,
        "createdAt": "2022-08-11T22:59:40.56995",
        "modifiedAt": "2022-08-11T22:59:40.56995",
        "condition": "USED",
        "details": {
          "body": "wood",
          "model": "crow",
          "manufacturer": {
            "code": 100,
            "name": "Audi"
          },
          "numberOfDoors": null,
          "fuelType": null,
          "engine": null,
          "mileage": null,
          "modelYear": null,
          "productionYear": null,
          "externalColor": null
        },
        "location": {
          "lat": 0,
          "lon": 0,
          "address": null,
          "city": null,
          "state": null,
          "zip": null
        },
        "price": null,
        "_links": {
          "self": {
            "href": "http://localhost:8080/cars/1"
          },
          "cars": {
            "href": "http://localhost:8080/cars"
          }
        }
      },
      {
        "id": 2,
        "createdAt": "2022-08-11T22:59:40.577338",
        "modifiedAt": "2022-08-11T22:59:40.577338",
        "condition": "USED",
        "details": {
          "body": "steel",
          "model": "house",
          "manufacturer": {
            "code": 100,
            "name": "Audi"
          },
          "numberOfDoors": null,
          "fuelType": null,
          "engine": null,
          "mileage": null,
          "modelYear": null,
          "productionYear": null,
          "externalColor": null
        },
        "location": {
          "lat": 0,
          "lon": 0,
          "address": null,
          "city": null,
          "state": null,
          "zip": null
        },
        "price": null,
        "_links": {
          "self": {
            "href": "http://localhost:8080/cars/2"
          },
          "cars": {
            "href": "http://localhost:8080/cars"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/cars"
    }
  }
}

 

Json 압축

<<json 나오는 명령어>> | jq -c .

$ curl http://localhost:8080/cars | jq -c .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1123    0  1123    0     0  93583      0 --:--:-- --:--:-- --:--:-- 93583
{"_embedded":{"carList":[{"id":1,"createdAt":"2022-08-11T22:59:40.56995","modifiedAt":"2022-08-11T22:59:40.56995","condition":"USED","details":{"body":"wood","model":"crow","manufacturer":{"code":100,"name":"Audi"},"numberOfDoors":null,"fuelType":null,"engine":null,"mileage":null,"modelYear":null,"productionYear":null,"externalColor":null},"location":{"lat":0,"lon":0,"address":null,"city":null,"state":null,"zip":null},"price":null,"_links":{"self":{"href":"http://localhost:8080/cars/1"},"cars":{"href":"http://localhost:8080/cars"}}},{"id":2,"createdAt":"2022-08-11T22:59:40.577338","modifiedAt":"2022-08-11T22:59:40.577338","condition":"USED","details":{"body":"steel","model":"house","manufacturer":{"code":100,"name":"Audi"},"numberOfDoors":null,"fuelType":null,"engine":null,"mileage":null,"modelYear":null,"productionYear":null,"externalColor":null},"location":{"lat":0,"lon":0,"address":null,"city":null,"state":null,"zip":null},"price":null,"_links":{"self":{"href":"http://localhost:8080/cars/2"},"cars":{"href":"http://localhost:8080/cars"}}}]},"_links":{"self":{"href":"http://localhost:8080/cars"}}}

 

결론

어영부영 세상 살다보니 Rest의 4단계 hateoas가 쓰이기 시작하네.. HAL이 Spring 때문에 이기려나

728x90
반응형