jq: count nest object values which satisfy condition


json data:

{testId: '1' studentId:{'s1':{score: 20} 's2':{score: 80}}} {testId: '2' studentId:{'s1':{score: 60} 's2':{score: 70}}} {testId: '3' studentId:{'s5':{score: 40} 's7':{score: 30}}} ...

I would like to use JQ to tell me how many students achieved a score > x, for each test.

Thus for the above input and x = 50, the output:

{testId: '1' numStudents:1} {testId: '2' numStudents:2} {testId: '3' numStudents:0}

I am able to generate a list of all students who achieved > 50 on each test with

{testId, studentId: .studentId[]?} | select(.studentId.score>50)

This command creates a list of objects where each object contains testId, studentId and score, where all scores are greater than 50. However I do not know how to recombine these results into the output I want.


The following assumes the input consists of a stream of valid JSON objects.

One approach is to use add:

$ jq '{testId, numStudents: ([select(.studentId[].score > 50) | 1] | add // 0)}'

With the given input (after corrections have been made), the output would be:

{ "testId": "1", "numStudents": 1 } { "testId": "2", "numStudents": 2 } { "testId": "3", "numStudents": 0 }

An alternative would be to use length, e.g. with this filter:

{testId, numStudents: ([select(.studentId[].score > 50) ] | length)}

However, if you don't mind adding a 'def', then the best solution (e.g. because it does not involve construction of an intermediate array) would be along the following lines:

def count(s): reduce s as $i (0; .+1); {testId, numStudents: count(select(.studentId[].score > 50))}


  • Access information inside Constructors
  • Null Pointer Exception during MySQL database write
  • Add Special Case to Power BI Query (M)
  • MySQL - get sum() grouped max() of group
  • How do I calculate a grouped z score in R using dplyr?
  • c# linear regression given 2 sets of data
  • Neo4j: Filter nodes based on aggregate function
  • Client side validation mvc dropdown
  • How to get the date of next specified day of week
  • Python cosine function precision [duplicate]
  • SetWindowsHookEx does not react on media keys
  • one Local Olampyad Questions on Informatic in 2011
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Deserializing XML into class C#
  • JSON with duplicate key names losing information when parsed
  • When should I choose bucket sort over other sorting algorithms?
  • what is the difference between the asp.net mvc application and asp.net web application
  • ORA-29908: missing primary invocation for ancillary operator
  • Jquery - Jquery Wysiwyg return html as a string
  • How to get next/previous record number?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • AT Commands to Send SMS not working in Windows 8.1
  • Matrix multiplication with MKL
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do you join a server to an Active Directory (domain)?
  • Why joiner is not used after Sequence generator or Update statergy
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • python draw pie shapes with colour filled
  • Add sale price programmatically to product variations
  • Is it possible to post an object from jquery to bottle.py?
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • Unable to use reactive element in my shiny app
  • Android Heatmap on canvas or ImageView
  • How do I use LINQ to get all the Items that have a particular SubItem?