ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [linux] jq 사용법
    Linux & Shell 2023. 9. 12. 22:07
    728x90
    반응형

    목차

     

     

     

    [json] key 로부터 value 추출

    echo '{"a":1}' | jq '.["a"]'
    
    >> 
    1

     

    [json] netsted json 의 value 추출

    echo '{"outer-key" : {"nested-key" : true}}' | jq '.["outer-key"]'
    
    >> 
    {
      "nested-key": true
    }
    
    echo '{"outer-key" : {"nested-key" : true}}' | jq '.["outer-key"] | .["nested-key"]'
    
    >> 
    true
    
    
    echo '{"outer-key" : {"nested-key" : {"inner-key": false}}}' | jq '.["outer-key"] | .["nested-key"] | .["inner-key"]' 
    
    >>
    false

     

     

    [json] list 추출하기

    echo '{"list" : [1,2,4,5,6,7,8,9,10]}' | jq '.["list"] | .[1]'
    
    >> 
    2
    
    
    echo '{"list" : [1,2,{"key": 3}, 4,5,6,7,8,9,10]}' | jq '.["list"] | .[2]'
    
    >> 
    {
      "key": 3
    }
    
    echo '{"list" : [1,2,{"key": 3}, 4,5,6,7,8,9,10]}' | jq '.["list"] | .[2] | .["key"]'
    
    >> 
    3

     

    [json] 배열 for loop

    for item in $(echo '{"list": [1,2,3,4,5,6,7,8,9]}' | jq -r '.["list"] | .[]')
    do
         echo $item
    done
    
    >> 
    1
    2
    3
    4
    5
    6
    7
    8
    9

     

    [json] key 추출하기

    echo '{"a":1, "b": 2}' | jq "keys"
    
    >>
    [
      "a",
      "b"
    ]

     

     

    반응형

    'Linux & Shell' 카테고리의 다른 글

    Linux date 알아보기  (0) 2023.09.20
    [Linux] SSH (Secure Shell)  (0) 2023.09.17
    Linux /etc/resolv.conf  (0) 2023.09.13
    Linux awk 알아보기  (0) 2021.12.04
    Linux File Descriptor  (0) 2019.07.25
Designed by Tistory.