Reputation: 25
This is the json respond body:
{
"studentName": "good student",
"age": "18",
"address": "street 123",
"courses": {
"math": {
"description": "how to calculate",
"enrollment": "enrolled",
"status": {
"result": "OK"
}
},
"english": {
"description": "abc",
"enrollment": "not-enrolled",
"status": {
"result": "OK"
}
}
}
}
I'd like to validate the "enrollment" value, for example, I want to get the output in the format:
math : enrolled
english : not-enrolled
wondering how to do this with jq command, thanks in advance.
Upvotes: 0
Views: 43
Reputation: 116780
If "validate" means checking that the values are all as expected, then you could use a filter along these lines:
.courses
| with_entries( select( .value.enrollment as $e
| ["enrolled", "not-enrolled"] | index($e) | not) )
That is, select the courses for which the "enrollment" is NOT in the list of acceptable values. (Here, "X | not" has the effect of negating X.)
Running this against your data augmented with a course for which the "enrollment" is invalid yields that course:
{
"french": {
"description": "abc",
"enrollment": "non",
"status": {
"result": "OK"
}
}
}
Upvotes: 0
Reputation: 134891
Not sure what you mean by "validate." But to simply get the "course"
and it's current "enrollment"
status, you could do this:
.courses | to_entries[] | "\(.key) : \(.value.enrollment)"
Upvotes: 1