4

I have a jsonb column where the structure always remains the same, but the keys within the json may change. For example,

{
   "key-12345": 
    {
       "values-12345": [
          {"type": 5200, 
           "source": "somesource", 
           "messageid": 707643203507, 
           "timestamp": "2018-07-26T21:25:42.612Z", 
           "destination": "somedestination", 
           "previouslyRouted": false
          }, 
          {"type": 5200, 
           "source": "anothersource", 
           "messageid": 707643203507, 
           "timestamp": "2018-07-26T21:26:01.542Z", 
           "destination": "anotherdestination", 
           "previouslyRouted": false
           }
         ]
       },

   "key-6789": 
    {
       "values-34512": [
          {"type": 5200, 
           "source": "yetantohersomesource", 
           "messageid": 707643203507, 
           "timestamp": "2018-07-26T21:25:42.612Z", 
           "destination": "yetanothersomedestination", 
           "previouslyRouted": false
          }, 
          {"type": 5200, 
           "source": "anothersource", 
           "messageid": 707643203507, 
           "timestamp": "2018-07-26T21:26:01.542Z", 
           "destination": "anotherdestination", 
           "previouslyRouted": false
           }
         ]
       }
}

I know that the structure of that document will be the same, but the keys could be anything. I can pull the keys themselves out with

select jsonb_object_keys(column) from table; 

easily enough but I don't know how to pull the object assigned to that key and deal with it. How do I select from a jsonb object based on the value of a key, rather than the values.

select object from document where json_key = 'key-12345';

1 Answer 1

0

This is how it's done:

SELECT object->>'key-12345' FROM document;

and with a where clause:

SELECT * FROM document WHERE object->>'key-12345' IS NOT NULL;
Sign up to request clarification or add additional context in comments.

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.