0

I write the query in postgresql I have store the data in json format of one column and now I need the particular value of the json

message_data :{"apiMessageId":"162fbbbe8f0e2a67","userEmail":"[email protected]","cloudId":1}

This is data and I want to only userEmail and this is my query

SELECT id, process_id, process_state, cast(message_data->> \'userEmail\' AS VARCHAR) AS userEmail, inserted_at, completed_at 
FROM realtimeincomingemailnotificationsqueue 
where api_message_id = '162fbbbe8f0e2a67';
2
  • cast(message_data->> 'userEmail')::varchar, but it is text anyway, so I dont see any need in additional cast Commented May 22, 2018 at 13:33
  • The backslash does not escape anything in SQL. You should use 'userEmail' rather than \'userEmail\' Commented May 22, 2018 at 13:37

1 Answer 1

1

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

->> text Get JSON object field as text

(emphasis mine)

so I don't why you would want to cast text to varchar, but if you really do, use

cast(message_data->> 'userEmail')::varchar
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.