Not sure I can do this but what I'm trying to do is create a new array by searching through some JSON data to find a key and taking the whole object into the new array if the key is found. Below is a sample of the data I am using.
{
"students": {
"AB10001": {
"campus": "cda",
"subjects": ["history", "english"],
}
"AB10002": {
"campus": "asd",
"subjects": ["maths"],
}
"AB10003": {
"campus": "asd",
"subjects": ["english"],
}
"AB10004": {
"campus": "asd",
"subjects": ["history"],
}
"AB10005": {
"campus": "cda",
"subjects": ["maths", "science"],
}
"AB10006": {
"campus": "asd",
"subjects": ["science"],
}
"AB10007": {
"campus": "cda",
"subjects": ["science"],
}
"AB10008": {
"campus": "asd",
"subjects": ["science", "history"],
}
"AB10009": {
"campus": "cda",
"subjects": ["history"],
}
"AB10010": {
"campus": "cda",
"subjects": ["history", "maths"],
}
}
}
So what I want to do is search through the students key of subjects
for all students who do history
and then create another array from that, taking the whole student, while leaving the original object the same.
So I want to end up with something that like this:
{
"historyStudents": {
"AB10001": {
"campus": "cda",
"subjects": ["history", "english"],
}
"AB10004": {
"campus": "asd",
"subjects": ["history"],
}
"AB10008": {
"campus": "asd",
"subjects": ["science", "history"],
}
"AB10009": {
"campus": "cda",
"subjects": ["history"],
}
"AB10010": {
"campus": "cda",
"subjects": ["history", "maths"],
}
}
}
Any insights or assistance would be very helpful. Thanks in advance!