extract property value from set of json arrays

Started by AC Gomezabout 6 years ago3 messagesgeneral
Jump to latest
#1AC Gomez
antklc@gmail.com

I have the following in a postgresql table
row 1: {"a": 1, "b": "[{"c": "123", "d":"456", "e": "789"}, {"c": "222",
"d":"111", "e": "000"} ]"}
row 2: {"a": 2, "b": "[{"c": "XXX", "d":"YYY", "e": "ZZZ"}, {"c": "666",
"d":"444", "e": "333"} ]"}

How do I pullout all "b":"e" values and end up with this result:
789
000
ZZZ
333

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: AC Gomez (#1)
Re: extract property value from set of json arrays

On Mon, Apr 6, 2020 at 7:21 PM AC Gomez <antklc@gmail.com> wrote:

I have the following in a postgresql table
row 1: {"a": 1, "b": "[{"c": "123", "d":"456", "e": "789"}, {"c": "222",
"d":"111", "e": "000"} ]"}
row 2: {"a": 2, "b": "[{"c": "XXX", "d":"YYY", "e": "ZZZ"}, {"c": "666",
"d":"444", "e": "333"} ]"}

How do I pullout all "b":"e" values and end up with this result:
789
000
ZZZ
333

Two approaches:

1. Wait for someone else to figure it out and give you the answer.

If 1. takes too long:

2. Read up on json operators.

https://www.postgresql.org/docs/12/functions-json.html

Then work out something that either works or gets you at least close. If
you are just close post the work done to date and any relevant points of
confusion.

I'll then likely be willing and able to fill in the missing gap(s) and
provide a relevant explanation.

You should formulate your query so that it doesn't require CREATE TABLE.
WITH vals (v) AS (VALUES (''::json)) SELECT vals.v FROM vals; makes
experimenting very easy.

Also, indicate which version of PostgreSQL you are working with.

David J.

#3AC Gomez
antklc@gmail.com
In reply to: David G. Johnston (#2)
Re: extract property value from set of json arrays

figured it out:

select unnest(array_agg(e.db ->> 'e')) as j
from tbl_t t
cross join lateral jsonb_array_elements((t.jdata->>'b')::jsonb)
as c(e)

On Mon, Apr 6, 2020 at 10:51 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:

Show quoted text

On Mon, Apr 6, 2020 at 7:21 PM AC Gomez <antklc@gmail.com> wrote:

I have the following in a postgresql table
row 1: {"a": 1, "b": "[{"c": "123", "d":"456", "e": "789"}, {"c": "222",
"d":"111", "e": "000"} ]"}
row 2: {"a": 2, "b": "[{"c": "XXX", "d":"YYY", "e": "ZZZ"}, {"c": "666",
"d":"444", "e": "333"} ]"}

How do I pullout all "b":"e" values and end up with this result:
789
000
ZZZ
333

Two approaches:

1. Wait for someone else to figure it out and give you the answer.

If 1. takes too long:

2. Read up on json operators.

https://www.postgresql.org/docs/12/functions-json.html

Then work out something that either works or gets you at least close. If
you are just close post the work done to date and any relevant points of
confusion.

I'll then likely be willing and able to fill in the missing gap(s) and
provide a relevant explanation.

You should formulate your query so that it doesn't require CREATE TABLE.
WITH vals (v) AS (VALUES (''::json)) SELECT vals.v FROM vals; makes
experimenting very easy.

Also, indicate which version of PostgreSQL you are working with.

David J.