Challenge Description

jq is a lightweight and flexible command-line JSON processor. In this programming challenge we will use it to process the grades of some students, that are saved in the file students.json

You can check the documentation of the jq tool anytime, via this link.


students.json
              
{"name": "Omar", "math": 10, "physics": 15, "english": 2}
{"name": "Mark", "math": 12, "physics": 18, "english": 15}
{"name": "Paul", "math": 8, "physics": 12, "english": 10}
{"name": "John", "math": 2, "physics": 2, "english": 1}
{"name": "Shakespear", "english": 20}
              
              
Part One

Write down some BASH command line, that calls jq tool, to filter out all the grades of the students from the file students.json, and show only the names, in the following compact form.

Expected Output:

              
{"name":"Omar"}
{"name":"Mark"}
{"name":"Paul"}
{"name":"John"}
{"name":"Shakespear"}
              
              

Show Hint 1

To print the whole content of a JSON file, you can use the command
jq '.' students.json


Show Hint 2

To print the Math grades of the students, use this command
jq -c '{math: .math}' students.json
ok, come on, it's easy now!


Show Answer

jq -c '{name: .name}' students.json

Part Two

Write down some BASH command line, that calls jq tool, to show only the students that have actually Maths grades.

Expected Output:

              
{"name":"Omar"}
{"name":"Mark"}
{"name":"Paul"}
{"name":"John"}
              
              

Show Hint 1

To filter elements, you can use the select(boolean expression) function


Show Hint 2

the function has(key) return true or false based on whether the object has a specific key.


Show Answer

jq -c 'select(has("math"))|{name: .name}' students.json