Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the value of a jsonb object as text with jsonb_path_query

From a json object i can get the value of the requestId as text with:

"request" ->> 'requestId'

with -> i get a json part

"request" -> 'requestId'

How does it works with the jsonb_path_query() to get the value as text?

jsonb_path_query(request :: jsonb, '$.requestId')

one solution should be:

select test1 ->> 0 
from ( 
 select jsonb_path_query(request :: jsonb, '$.journeys[*].costs.distance.value') test1,
        jsonb_path_query(request :: jsonb, '$.journeys[*].costs.distance.unit') test2 
 from json_import ri
) foo;

or is there a smarter way to get the same answer?

like image 982
Tibor Avatar asked Nov 18 '25 04:11

Tibor


1 Answers

Extract the string from the resulting jsonb:

SELECT j ->> 0
FROM jsonb_path_query(request::jsonb, '$.requestId') AS j(j);
like image 110
Laurenz Albe Avatar answered Nov 20 '25 18:11

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!