Filter JSON Column Values
DB2Rest supports applying filter on JSON or JSONB columns of PostgreSQL
database. For this DB2Rest has extended the RSQL operator list
to add JSON specific operators.
jbe
The jbe
operator performs an equality check on a certain attribute. Lets retrieve all the products where brand is Apple
.
- cURL
- HTTPie
curl --request GET \
--url 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':name,data->>'price':price&filter=data**brand=jbe=Apple' \
--header 'User-Agent: insomnia/9.2.0'
http GET 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':name,data->>'price':price&filter=data**brand=jbe=Apple' \
User-Agent:insomnia/9.2.0
The query above returns the following result:
[
{
"id": 1,
"name": "iPhone 15 Pro",
"price": "999.99"
},
{
"id": 3,
"name": "Leather Case for iPhone 15 Pro",
"price": "69.99"
}
]
jsonbContain
This filter operator checks if a JSON document contains another JSON document.
The following DB2Rest query will retrieve products where category is Electronics
.
- cURL
- HTTPie
curl --request GET \
--url 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':product_name&filter=data=jsonbContain='{"category": "Electronics"}' \
--header 'User-Agent: insomnia/9.2.0'
http GET 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':product_name&filter=data=jsonbContain='{"category": "Electronics"}' \
User-Agent:insomnia/9.2.0
The query above returns the following result:
[
{
"id": 1,
"product_name": "iPhone 15 Pro"
},
{
"id": 2,
"product_name": "Samsung Galaxy Watch 4"
}
]
jsonContain
Now let us retrieve the product whose price is 999.99
- cURL
- HTTPie
curl --request GET \
--url 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':name,data->>'price':price&filter=data=jsonContain='{"price": 999.99}' \
--header 'User-Agent: insomnia/9.2.0'
http GET 'http://localhost:8080/v1/rdbms/pgdb/products?fields=id, data->>'name':name,data->>'price':price&filter=data=jsonContain='{"price": 999.99}' \
User-Agent:insomnia/9.2.0
The result is shown below:
[
{
"id": 1,
"name": "iPhone 15 Pro",
"price": "999.99"
}
]