Need to return payload.* json path where condition payload...cities.name = city_x matches. Tried looking google cel or jq for this case. did not find way for this so far.
Case 1 – Sample Json Input
{
"payload": {
"book1": {
"isbn": "123-456-2221",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
],
"sellerid1": {
"name": "sellerid1",
"cities": [{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_y",
"sellcount": 200
}
]
},
"sellerid2": {
"name": "sellerid2",
"cities": [{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_b",
"sellcount": 15
}
]
}
},
"book2": {
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "C programming guide",
"category": [
"Programming",
"Technology"
],
"sellerid3": {
"name": "sellerid3",
"cities": [{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_b",
"sellcount": 200
}
]
},
"sellerid4": {
"name": "sellerid4",
"cities": [{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_x",
"sellcount": 15
}
]
}
}
}
}
######## Sample json case 2 ###############
{
"payload": {
"book1": {
"isbn": "123-456-2221",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
],
"sellers": {
"sellerid1": {
"name": "sellerid1",
"cities": [{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_y",
"sellcount": 200
}
]
},
"sellerid2": {
"name": "sellerid2",
"cities": [{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_b",
"sellcount": 15
}
]
}
}
},
"book2": {
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "C programming guide",
"category": [
"Programming",
"Technology"
],
"sellers": {
"sellerid3": {
"name": "sellerid3",
"cities": [{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_b",
"sellcount": 200
}
]
},
"sellerid4": {
"name": "sellerid4",
"cities": [{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_x",
"sellcount": 15
}
]
}
}
},
"book3": {
"isbn": "123-456-2221",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
]
},
"book4": {
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "C programming guide",
"category": [
"Programming",
"Technology"
],
"sellers": {}
}
}
}
required output is same for case1 and case2.
{
"payload": {
"book2": {
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "C programming guide",
"category": [
"Programming",
"Technology"
],
"sellerid3": {
"name": "sellerid3",
"cities": [{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_b",
"sellcount": 200
}
]
},
"sellerid4": {
"name": "sellerid4",
"cities": [{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_x",
"sellcount": 15
}
]
}
}
}
}
Tried CEL to filter , CEL playground. Nested filtering this style did not find option in CEL. Looking if any other way using jq.
This keeps all those fields under .payload
that have at least one path .[].cities[].name
that is resolvable and evaluates to "city_x"
. Consequently, other fields are removed.
.payload |= with_entries(select(IN(.value[].cities?[]?.name; "city_x")))
{
"payload": {
"book2": {
"isbn": "123-456-222",
"author": {
"lastname": "Doe",
"firstname": "Jane"
},
"editor": {
"lastname": "Smith",
"firstname": "Jane"
},
"title": "C programming guide",
"category": [
"Programming",
"Technology"
],
"sellerid3": {
"name": "sellerid3",
"cities": [
{
"name": "city_a",
"sellcount": 100
},
{
"name": "city_b",
"sellcount": 200
}
]
},
"sellerid4": {
"name": "sellerid4",
"cities": [
{
"name": "city_a",
"sellcount": 10
},
{
"name": "city_x",
"sellcount": 15
}
]
}
}
}
}
corrected the json.