I have a jsonb "contacts" field in a table which is:
- An object, where each key is the contact type such as mailing, billing, emergency etc. These are loosely defined.
- The value for each key in #1 is an array containing 1 or more contacts for each type
- Each value inside #2 is an object containing loosely defined fields.
Example:
{
"main": [{
"name": "Bobby Smith",
"email": "[email protected]",
"telephone": "555-999-0000"
}, {
"name": "Joan Smith",
"email": "[email protected]",
"telephone": "555-999-0002"
}],
"billing": [{
"name": null,
"region": "Arizona",
"address": ["PO BOX 123", "456 Nowhere Road"],
"country": "USA",
"locality": "BigMac",
"postalCode": "90210"
}],
"emergency": [{
"name": "John Cooper",
"email": "[email protected]",
"telephone": "555-987-0000"
}]
}
I'd like to have a simple way to do a comparison on say the name fields that is agnostic to the keys in the outermost object, as well as the number of elements in each array.
Effectively this: SELECT * FROM clients WHERE contacts#>>'{*, *, name}' = 'John Cooper';
Would return the row containing the above as it matches '{emergency, 0, name}'