How to search a string inside a json structure

Started by Sami Pietiläover 10 years ago8 messagesgeneral
Jump to latest
#1Sami Pietilä
sami.pietila@gmail.com

Hi,

I have a database with jsonb type of columns. Colums contain complex json
structures. I would like to get all rows which contain a json where any of
json's values matches to a given string (like %hello%).

How to create a postgre sql query to do this?

I guess postgre should traverse though each json structures while finding
the string.

Thanks

#2Jason O'Donnell
odonnelljp01@gmail.com
In reply to: Sami Pietilä (#1)
Re: How to search a string inside a json structure

Sami,

What version of postgres are you using?

There's some examples using GIN indexes for searching jsonb objects in the
wiki:
https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.4#JSONB_Binary_JSON_storage

Hope that helps,

On Mon, Nov 2, 2015 at 4:09 PM, Sami Pietilä <sami.pietila@gmail.com> wrote:

Hi,

I have a database with jsonb type of columns. Colums contain complex json
structures. I would like to get all rows which contain a json where any of
json's values matches to a given string (like %hello%).

How to create a postgre sql query to do this?

I guess postgre should traverse though each json structures while finding
the string.

Thanks

--
Jason O'Donnell
Crunchy Data Solutions

#3Sami Pietilä
sami.pietila@gmail.com
In reply to: Jason O'Donnell (#2)
Re: How to search a string inside a json structure

Hi,

Thank you for reply!

I am using version 9.4.5.

Unfortunately I could not figure out how to select rows which, for example,
contain following json: '{"a":"world","c":{"b":"helloworld"}}' by search
with "hello" string.

I am trying to create a query which looks values in any field in JSON, "a"
and "b" in this case (without knowing the names "a" and "b").

Thanks

2015-11-03 0:56 GMT+02:00 Jason O'Donnell <odonnelljp01@gmail.com>:

Show quoted text

Sami,

What version of postgres are you using?

There's some examples using GIN indexes for searching jsonb objects in the
wiki:
https://wiki.postgresql.org/wiki/What&#39;s_new_in_PostgreSQL_9.4#JSONB_Binary_JSON_storage

Hope that helps,

#4Vick Khera
vivek@khera.org
In reply to: Sami Pietilä (#3)
Re: How to search a string inside a json structure

On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com>
wrote:

Unfortunately I could not figure out how to select rows which, for
example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by
search with "hello" string.

cast the field to a text:

select * from t where myfield::text like '%hello%';

#5Merlin Moncure
mmoncure@gmail.com
In reply to: Vick Khera (#4)
Re: How to search a string inside a json structure

On Tue, Nov 3, 2015 at 9:57 AM, Vick Khera <vivek@khera.org> wrote:

On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com>
wrote:

Unfortunately I could not figure out how to select rows which, for
example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by
search with "hello" string.

cast the field to a text:

select * from t where myfield::text like '%hello%';

Performance of this will be awful. Unfortunately, FWICT there is no
way to do partial string matches against json in a indexed way without
some serious elbow grease. For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module. It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6Sami Pietilä
sami.pietila@gmail.com
In reply to: Merlin Moncure (#5)
Re: How to search a string inside a json structure

Hi,

Thank you for reply.

I think I need to do some more research of means to implement searching for
json databases.

I'll look the module.

Thanks

2015-11-03 18:43 GMT+02:00 Merlin Moncure <mmoncure@gmail.com>:

Show quoted text

On Tue, Nov 3, 2015 at 9:57 AM, Vick Khera <vivek@khera.org> wrote:

On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com>
wrote:

Unfortunately I could not figure out how to select rows which, for
example, contain following json: '{"a":"world","c":{"b":"helloworld"}}'

by

search with "hello" string.

cast the field to a text:

select * from t where myfield::text like '%hello%';

Performance of this will be awful. Unfortunately, FWICT there is no
way to do partial string matches against json in a indexed way without
some serious elbow grease. For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module. It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin

#7Sami Pietilä
sami.pietila@gmail.com
In reply to: Merlin Moncure (#5)
Re: How to search a string inside a json structure

Hi,

I have simplified the case and tried to look from a list of json items
(please see test table content). I think I have managed to unpack values in
such a way that also partial matching is possible. However, the end result
has two "value" named columns even if I have tried to rename them.

How can I rename the columns coming from jsonb_ functions? Or is there a
better way of unpacking the list and items?

select * from test;
id | main
----+--------------------------
1 | [{"A": "b"}, {"B": "b"}]
(1 row)

select * from test, jsonb_array_elements(test.main) as m, jsonb_each(value);
id | main | value | key | value
----+--------------------------+------------+-----+-------
1 | [{"A": "b"}, {"B": "b"}] | {"A": "b"} | A | "b"
1 | [{"A": "b"}, {"B": "b"}] | {"B": "b"} | B | "b"
(2 rows)

Thanks

Performance of this will be awful. Unfortunately, FWICT there is no

Show quoted text

way to do partial string matches against json in a indexed way without
some serious elbow grease. For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module. It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin

#8David G. Johnston
david.g.johnston@gmail.com
In reply to: Sami Pietilä (#7)
Re: How to search a string inside a json structure

On Saturday, November 7, 2015, Sami Pietilä <sami.pietila@gmail.com> wrote:

How can I rename the columns coming from jsonb_ functions? Or is there a
better way of unpacking the list and items?

select * from function(...) [as] func-name_alias (col1_alias, col2_alias,
etc...)

David J.