0

Trying to concat two json text fields like so:

SELECT app->>'id' || app->>'version' as foo
FROM table,
 json_array_elements(applications::json) as app;

Where applications is the data type text, results in the error:

LINE 3: SELECT app->>'id' || app->>'version' as foo
 ^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

I've also tried with jsonb. What is happening here, and how should json text fields be concatenated together?

asked Jan 26, 2023 at 18:19
3
  • You just need to cast the values to the appropriate data types. Try something like this SELECT (app->>'id')::text || app->>'version' as foo Commented Jan 26, 2023 at 18:25
  • If the columnapplications really contains JSON values, it should be declared as a jsonb column so that you don't need to cast it every time you want to do something useful with it. Commented Jan 26, 2023 at 18:47
  • 1
    You have a precedence problem. Your expression is interpreted as (app->>'id' || app)->>'version'. Add explicit parentheses to clarify the order of the operators. Commented Jan 26, 2023 at 18:49

1 Answer 1

2

I think || binds stronger than the ->> operator, so your expression is evaluated as app ->> ('id'||app) ->> 'version'

Enclose each expression in parentheses to avoid this:

(app->>'id') || (app->>'version')
answered Jan 26, 2023 at 18:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.