1

I am trying to parse following json data and use it under the where clause.

Basic":{  
   "General":{  
      "Field1":1234,
      "Field2":"6.86"
   },
   "Stream 0":{  
      "Type":"LDAP",
      "Field4":"LALA1"
    },
    "Stream 1":{  
      "Type":"KERBEROS",
      "Field4":"LALA2"
    },
    "Stream 2":{  
      "Type":"SAML",
      "Field4":"LALA3"
    },

I can reach to Type tag like this.
table.column_json::json->'Basic'->'Stream 0'->'Type'

The order of Stream # is undefined and there could more than 3 Stream tags.

How can I iterate through each Stream and find if Type: is "SAML" then Field4 value is "LALA3".

1 Answer 1

1

Since you have multiple different keys you want to search through, you need to "unpack" the K/V pairs and then do string matching:

SELECT DISTINCT t.*
FROM table t, json_each(t.column_json::json->'Basic') j(k, v)
WHERE j.k LIKE 'Stream%'
  AND j.v->>'Type' = 'SAML'
  AND j.v->>'Field4' = 'LALA3';

Note that the json_each() function is PG9.3+.

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

2 Comments

json_each() worked for me but = are giving operator does not exist error. I tried using ` j.v->'Type'::varchar='SAML'` but no luck.
Ah, yes. It should have been the ->> operator to get the field as text. See updated answer.

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.