Tuesday, July 19, 2016

JQ (jq) JSON parser for BASH (linux) command line

JQ is awesome tool to traverse through the JSON and present the data the way you like it.
Below $i and $j come from a for loop ($j is arch type) ($i is 0..20) array

jq -r "{ os: .$j|to_entries|values[$i].key, gpu: .$j|to_entries| values[$i].value.null[] }  " /tmp/farm.json

Example JSON:
 {
   "ppc64le" : {                  
       "RHEL7_2" : {              
           "null" : [             
               "gpu1",           
               "gpu2",            
               "gpu3"             
           ]                      
       },                         
       "Ubuntu15_04" : {          
           "null" : null          
       },                         
       "Ubuntu16_04" : {          
           "null" : [             
               "gpu1",           
               "gpu2"             
           ]                      
       }                          
   },                             
   "x86" : {                      
       "FC22" : {                 
           "null" : null          
       },                         
       "FC23" : {                 
           "null" : null          
       }
}                        

FUNCTION

function farm_config_list {                                                                                                               
       echo -e "\n\n------\n- Generating config list \n As of: $(date)\n-------\n"                       
        tool --query farm > /tmp/farm.json \                                                                              
       && sed -i 's/FARM = //g' /tmp/farm.json \                                                                                         
       && for j in aarch64 ARMv7 x86 ppc64le x86_64; \                                                                                        
                do for i in {0..90}; \                                                                                                        
                       do cat /tmp/farm.json |jq -r "{ os: .$j|to_entries|values[$i].key, gpu: .$j|to_entries| values[$i].value.null[] }  " \
                               |tr '\n' ' ' \                                                                                                 
                               |sed -e 's/{/\n{/g' -e 's/"os"/"arch": '"$j"',\t"os"/g' -e 's/{/\t\t{/g';\                                     
                        done; \                                                                                                               
                done 2>/dev/null \                                                                                                            
       |grep -v "jq: error"                                                                                                                   
                                                                                                                                              
}