How to search a specific string in JSON or JSONB array in PostgreSQL ?
I have a column called meta_data whose data type is JSONB data type which deals with JSON array. And let’s say you wanted to find rows that contains the string “deviceid” in the meta_data column (JSONB data type) in audit trail database table. Let’s learn how to write query to search for specific string in JSONB array.
Search for specific string in JSONB array
If you are using PostgreSQL 11 or older you can use the following query to search for a text in database column of JSONB data type.
select * from audittrail where meta_data::text like '%deviceid%'; --(OR) select * from audittrail where meta_data::text like '%"deviceid"%'; -- if you are looking for the exact value
In case if you are using PostgreSQL 12 and above, then you can use the following query to search for a string in JSONB or JSON array.
select * from audittrail where jsonb_path_exists(meta_data, '$.** ? (@.type() == "string" && @ like_regex "deviceid")')
Query Explanation:
- $.** — find any value at any level
- ? — where
- @.type() == “string” — value is string
- && — and
- @ like_regex “deviceid” — value contains ‘deviceid’
What is JSONB data type ?
PostgreSQL added support for jsonb data type in version 9.4. Note, that the letter ‘b’ at the end stands for ‘better’.
Note, there are two JSON data types in PostgreSQL 9.4: json and jsonb. They almost accept similar values as input, but the major difference is efficiency.
The json data type stores the exact copy of the text provided as input and its processing functions requires to reparse the data on each execution. But, the jsonb data is stored in special binary format and this format is compressed and more efficient than the text format. And regarding data processing for jsonb it is significantly faster compared to json as it does not require reparsing. Also, jsonb supports indexing and this can be significant advantage.
To know more about the JSON and JSONB functions and operators please refer this JSON functions and operators.
Hope this sneppet on how to search for specific string in JSONB is helpful 🙂
You may also like:
- Visualize database tables and relations in postgreSQL using PgAdmin ?
- How to Start Stop Restart MariaDB on Linux OS ?
- MariaDB – How to set max_connections permanently ?
- Find mongo documents where a field contains a string pattern
- How to set or change root password in Ubuntu Linux ?
- Putty Fatal Error No supported authentication methods available
- How to find which users belongs to a specific group in linux
- Give write permissions for specific user or group for specific folder in linux
- MongoDB equivalent command for SQL’s between numbers, text or dates
- Sort numeric strings as numbers in mongodb database
- GCP Virtual Private Cloud (VPC) Networks Fundamentals
- Sort numeric strings as numbers in mongodb database
- Is it possible to change Google Cloud Platform Project ID ?
- Create non-root SSH user account and provide access to specific folders
- What is the purpose of Git Stash command ?
- The pgAdmin 4 PostgreSQL server could not be contacted
References