jq not working on tag name with dashes and numbers

You need to enclose in brackets and double quotes: jq ‘.”component-status”‘ With your given input it returns: [ { “status”: “OK”, “component”: “Service1”, “status-code”: 200 }, { “status”: “OK”, “component”: “Service2”, “status-code”: 200 } ] The jq Manual (development) –> Basic filters: .foo, .foo.bar The simplest useful filter is .foo. When given a JSON object … Read more

Add new element to existing JSON array with jq

The |= .+ part in the filter adds a new element to the existing array. You can use jq with filter like: jq ‘.data.messages[3] |= . + {“date”: “2010-01-07T19:55:99.999Z”, “xml”: “xml_samplesheet_2017_01_07_run_09.xml”, “status”: “OKKK”, “message”: “metadata loaded into iRODS successfullyyyyy”}’ inputJson To avoid using the hardcoded length value 3 and dynamically add a new element, use … Read more

Passing bash variable to jq

Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration): projectID=$(jq -r –arg EMAILID “$EMAILID” ‘ .resource[] | select(.username==$EMAILID) | .id’ file.json) Postscript For the record, another possibility would be to use jq’s env function for accessing environment variables. For example, consider this sequence of … Read more

How to sort json data by date and time

You could use sort_by/1 on the fields, you’ll have to either normalize the date/time formats or conditionally parse them out using strptime/1. It returns an array of the date fields in significant order combine to a single array and sort_by will do the rest. .data.monitor | sort_by([ (.Date | strptime(“%A %d %B %Y”)), (.Time | … Read more