Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count number of elements after query?

Tags:

jq

I have a query:

.modules[].resources | select (.[]!=null)

and after it I have got:

{ somestuff } { somestuff } { somestuff }

when I add legth after all:

.modules[].resources | select (.[]!=null) | length

I have got:

1 1 1

but I need to count elements, so I need 3 in an output. How can I implement it ?

In fact it would be very useful to create an array from the first query output to operate with it furthure

[ { somestuff } , { somestuff } , { somestuff } ]
like image 789
Konstantin Shestakov Avatar asked Sep 28 '17 03:09

Konstantin Shestakov


People also ask

How do you count the number of elements in an array?

//Number of elements present in an array can be calculated as follows. int length = sizeof(arr)/sizeof(arr[0]);

How do you count the number of elements in an array in Python?

Python len() method enables us to find the total number of elements in the array/object. That is, it returns the count of the elements in the array/object.

How do you count the number of elements in an array in C++?

Get Number of Elements in Array in C++The sizeof() function in C++ returns the size of the variable during compile time. Arrays store elements of the same type. So, we can find out the total size of the array and divide it by the size of any element of the array to calculate its length.


2 Answers

You can put the results of the query into a list and get the length of this list:

[ .modules[].resources | select (.[]!=null) ] | length
like image 82
hek2mgl Avatar answered Oct 22 '22 16:10

hek2mgl


Since you indicated it would be very useful to create an array from the first query output, what you probably want to use here is map.

Let's assume your data is something like the data from this question: jq: search by value from another array element

{
  "modules": [
    {
      "resources": [
        {
          "type": "openstack_compute_instance_v2",
          "primary": {
            "id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
            "attributes": {
              "name": "jumpbox"
            }
          }
        },
        {
          "type": "openstack_compute_floatingip_associate_v2",
          "primary": {
            "attributes": {
              "instance_id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
              "floating_ip": "10.120.241.21"
            }
          }
        }
      ]
    }
  ]
}

with this data your filter

   .modules[].resources 
 | select (.[]!=null)    #< do you really want this `[]` ?

would produce two copies of the entire .resources array. What you may want instead is

   .modules[].resources 
 | map(select(.!=null))

which will give you an array

[
  {
    "type": "openstack_compute_instance_v2",
    "primary": {
      "id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
      "attributes": {
        "name": "jumpbox"
      }
    }
  },
  {
    "type": "openstack_compute_floatingip_associate_v2",
    "primary": {
      "attributes": {
        "instance_id": "5edfe2bf-94df-49d5-8118-3e91fb52946b",
        "floating_ip": "10.120.241.21"
      }
    }
  }
]

to get the length, just add length:

  .modules[].resources
| map(select(.!=null))
| length

in this example giving

2

Because map(f) is defined as [ .[] | f ] the above filter is really

  .modules[].resources
| [
      .[]
    | select(.!=null)
  ]
| length

In this form you can clearly see the construction of the intermediate array.

Note also that jq provides a built-in values filter which is defined as select(.!=null) so this example could be further simplified to just

  .modules[].resources
| map(values)
| length
like image 2
jq170727 Avatar answered Oct 22 '22 14:10

jq170727