json - JQ: Show the contents/value of a single-element array -


my json file:

{     "metricalarms": [         {             "evaluationperiods": 2,             "alarmactions": [                 "this causing me trouble"             ],             "threshold": 0.0,             "dimensions": [                 {                     "name": "dbinstanceidentifier",                     "value": "db-master02"                 }             ],             "metricname": "replicalag"         }     ] } 

my current command flatten json file (jq 1.3, cannot upgrade right now):

cat $json_file |    jq -r '.metricalarms[] |           { metricname,             threshold,             evaluationperiods,             alarmactions           } +           ( .dimensions[] | { dimname: .name, dimvalue: .value } )    ' 

produces:

{   "dimname": "dbinstanceidentifier",   "dimvalue": "db-master02",   "alarmactions": [     "this causing me trouble"   ],   "evaluationperiods": 2,   "threshold": 0,   "metricname": "replicalag" } 

what want:

{   "dimname": "dbinstanceidentifier",   "dimvalue": "db-master02",   "alarmactions": "this causing me trouble",   "evaluationperiods": 2,   "threshold": 0,   "metricname": "replicalag" } 

i want show value "alarmactions" without array.

this seems simple problem, answer eludes me. have tried treating "alarmactions" in manner similar way treat "dimensions". have tried every variation of [], [0], periods, pipes, , function calls can think of, no avail. missing?

use:

cat $json_file | jq -r '.metricalarms[] |       { metricname,         threshold,         evaluationperiods,         "alarmactions" : .alarmactions[0]       } +       ( .dimensions[] | { dimname: .name, dimvalue: .value } ) ' 

i.e. don't use shortcut syntax alarmactions element.


Comments