Friday, January 15, 2016

More jq magic for JSON wrangling


Count number of JSON elements in an embedded array

Data out of an Elasticsearch aggregation result set: 

{
  "aggregations": {
    "flightdestination": {
      "buckets": [
        {
          "sentiment": {
            "value": 2.1706734237483767
          },
          "doc_count": 6931,
          "key": "newark"
        },
        {
          "sentiment": {
            "value": 2.17875893247776
          },
          "doc_count": 6857,
          "key": "houston"

        },
...

curl -s 'http://10.60.35.34:9200/1$1_0/sentence/_search' -XGET --data "$(cat ~/query.json)" | jq '.aggregations.flightdestination.buckets | length'