Similar to this problem except I am trying to include objects before and after the matched objects.
So for example I want to find all objects with type.name='pass', plus any object that is within X (say 2) of this object, either before or after.
This JSON:
[
{
"class": "Something1",
"type": {
"name": "Foul"
}
},
{
"class": "Something2",
"type": {
"name": "Carry"
}
},
{
"class": "Something3",
"type": {
"name": "Pass"
}
},
{
"class": "Something4",
"type": {
"name": "Pass"
}
},
{
"class": "Something5",
"type": {
"name": "Carry"
}
},
{
"class": "Something6",
"type": {
"name": "Carry"
}
},
{
"class": "Something7",
"type": {
"name": "Other"
}
},
{
"class": "Something8",
"type": {
"name": "Other"
}
},
{
"class": "Something9",
"type": {
"name": "Carry"
}
},
{
"class": "Something10",
"type": {
"name": "Pass"
}
},
{
"class": "Something1",
"type": {
"name": "Carry"
}
},
{
"class": "Something2",
"type": {
"name": "Carry"
}
},
{
"class": "Something3",
"type": {
"name": "Carry"
}
},
{
"class": "Something4",
"type": {
"name": "Other"
}
},
{
"class": "Something5",
"type": {
"name": "Carry"
}
}
]
Would output a new JSON string:
[
{
"class": "Something1",
"type": {
"name": "Foul"
}
},
{
"class": "Something2",
"type": {
"name": "Carry"
}
},
{
"class": "Something3",
"type": {
"name": "Pass"
}
},
{
"class": "Something4",
"type": {
"name": "Pass"
}
},
{
"class": "Something5",
"type": {
"name": "Carry"
}
},
{
"class": "Something6",
"type": {
"name": "Carry"
}
},
{
"class": "Something8",
"type": {
"name": "Other"
}
},
{
"class": "Something9",
"type": {
"name": "Carry"
}
},
{
"class": "Something10",
"type": {
"name": "Pass"
}
},
{
"class": "Something1",
"type": {
"name": "Carry"
}
},
{
"class": "Something2",
"type": {
"name": "Carry"
}
}
]
Or it could output the index of the above objects in a list which can then be used to search the original JSON.
I can filter by "type.name" thanks to the answer quoted above, but I could not work out how to include surrounding objects.
$ passes=$(cat file.json | jq -c '[ .[] | select( .type.name | contains("Pass")) ]')
The files I am working with are 140,000+ lines long so efficiency is important.
Edit: Thanks @Gilles Quenot for fixing the code formatting.
Edit: Corrected errors in JSON and explained approach taken so far.