0

I have jsonb column feeds with data in the form of this:

[{
  "id": 99999999,
  "lang": "pt",
  "entities": {
    "urls": [],
    "media": [{
      "id": 123456456,
      "type": "photo",
      "id_str": "123456456",
      "indices": [37, 59],
    }]
  },
  "favorited": false,
  "retweeted": false,
  "truncated": false
}]

How do I query the first media ->> id since it's in an json array? So that the result is like this:

   id             media_id
---------------------------
99999999         123456456

I tried jsonb_array_elements but can't seem to get the output I want, especially the part where the media is nested within another array.

2 Answers 2

2

Figured it out

SELECT
  jsonb_array_elements(feeds)->>'id',
  (((jsonb_array_elements(feeds)->'entities')->'media')->0)->>'id' as media
FROM mytable
Sign up to request clarification or add additional context in comments.

Comments

0
select feeds::jsonb->0->>'entities'->>'media'->0->>'id' from table;

Comments

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.