0

I have a strange JSON array in my Postgres Database without curly brackets. It has the datatype Text so i need to cast it in a JSON i guess.

It looks like this and can change from row to row.

    [
  [
    "-",
    "name",
    "Gates"
  ],
  [
    "-",
    "name_1",
    null
  ],
  [
    "-",
    "name_2",
    null
  ],
  [
    "-",
    "na_cd",
    null
  ],
  [
    "-",
    "class_cd",
    null
  ],
  [
    "-",
    "reference",
    "190955"
  ],
  [
    "-",
    "lang_cd",
    "en"
  ],
  [
    "-",
    "uid_nr",
    null
  ],
  [
    "-",
    "id",
    19000
  ],
  [
    "-",
    "firstname",
    "Bill"
  ],
  [
    "-",
    "spare",
    null
  ]
]

What i need is to find and print the ID if there is one. In this example 19000.

Can someone please help how can do this?

1
  • What if there is more than one array element with id ? Commented Sep 28, 2021 at 15:26

2 Answers 2

2

Basically, you should use jsonb_array_elements() twice, for the main array and for its filtered element (which is an array too).

select value::numeric as result
from (
    select elem
    from the_data
    cross join jsonb_array_elements(col) as main(elem)
    where elem ? 'id'
    ) s
cross join jsonb_array_elements(elem)
where jsonb_typeof(value) = 'number'

Try it in Db<>Fiddle.

However, if you want to get exactly the third value from the nested array, the query may be simpler (note that array elements are indexing from 0):

select (elem->2)::numeric as result
from the_data
cross join jsonb_array_elements(col) as main(elem)
where elem ? 'id'

Db<>Fiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

If you are using Postgres 12 or later and if the value you are after is always at the third array element, you can use a SQL/JSON path expression:

select jsonb_path_query_first(the_column, '$ ? (@[*] == "id")[2]')::int
from the_table

This assumes that the column is defined as jsonb (which it should be). If it's not, you need to cast it: the_column::jsonb

3 Comments

ERROR: function jsonb_path_query_first(jsonb, unknown) does not exist :(
@user2210516: I assume you are using a current Postgres version. Which version are you using?
PostgreSQL 10.18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.