pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedGetting json-value as varchar
11+ messages / 5 participants
[nested] [flat]
* Getting json-value as varchar
@ 2022-01-06 12:28 Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 14:39 ` Re: Getting json-value as varchar Thomas Kellerer <shammat@gmx.net>
0 siblings, 2 replies; 11+ messages in thread
From: Andreas Joseph Krogh @ 2022-01-06 12:28 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi, in PG-14 this query returns "value" (with double-quotes):
SELECT ('{"key":"value"}'::jsonb)['key'];
┌─────────┐
│ jsonb │
├─────────┤
│ "value" │
└─────────┘
(1 row)
and this returns 'value' (without the quotes):
SELECT ('{"key":"value"}'::jsonb)->> 'key';
┌──────────┐
│ ?column? │
├──────────┤
│ value │
└──────────┘
(1 row)
How to I use the subscript syntax and get the result as varchar instead of
JSONB, assuming Iknow the JSON-field is a String?
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 12:31 ` Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
1 sibling, 1 reply; 11+ messages in thread
From: Thomas Markus @ 2022-01-06 12:31 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
Am 06.01.22 um 13:28 schrieb Andreas Joseph Krogh:
> Hi, in PG-14 this query returns "value" (with double-quotes):
> SELECT ('{"key":"value"}'::jsonb)['key'];
> ┌─────────┐
> │ jsonb │
> ├─────────┤
> │ "value" │
> └─────────┘
> (1 row)
>
> and this returns 'value' (without the quotes):
> SELECT ('{"key":"value"}'::jsonb)->> 'key';
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value │
> └──────────┘
> (1 row)
>
> How to I use the subscript syntax and get the result as varchar
> instead of JSONB, assuming I /know/ the JSON-field is a String?
simply cast your value
SELECT (('{"key":"value"}'::jsonb)->> 'key')::text;
best regards
Thomas
> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com <https://www.visena.com;
> <https://www.visena.com;
Attachments:
32yBiSYBRxPXpqis.png (image/png, 1.9K) download
view image
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
@ 2022-01-06 12:36 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:25 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
0 siblings, 2 replies; 11+ messages in thread
From: Andreas Joseph Krogh @ 2022-01-06 12:36 UTC (permalink / raw)
To: Thomas Markus <t.markus@proventis.net>; +Cc: pgsql-general@lists.postgresql.org
På torsdag 06. januar 2022 kl. 13:31:19, skrev Thomas Markus <
t.markus@proventis.net <mailto:t.markus@proventis.net>>: Hi,
Am 06.01.22 um 13:28 schrieb Andreas Joseph Krogh:
Hi, in PG-14 this query returns "value" (with double-quotes):
SELECT ('{"key":"value"}'::jsonb)['key'];
┌─────────┐
│ jsonb │
├─────────┤
│ "value" │
└─────────┘
(1 row)
and this returns 'value' (without the quotes):
SELECT ('{"key":"value"}'::jsonb)->> 'key';
┌──────────┐
│ ?column? │
├──────────┤
│ value │
└──────────┘
(1 row)
How to I use the subscript syntax and get the result as varchar instead of
JSONB, assuming Iknow the JSON-field is a String? simply cast your value
SELECT (('{"key":"value"}'::jsonb)->> 'key')::text;
best regards
Thoma
I think you misread my message. What I want is for the subscript-version:
('{"key":"value"}'::jsonb)['key']
to return:
┌──────────┐
│ ?column? │
├──────────┤
│ value │
└──────────┘
instead of
┌─────────┐
│ jsonb │
├─────────┤
│ "value" │
└─────────┘
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 13:13 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
1 sibling, 1 reply; 11+ messages in thread
From: David G. Johnston @ 2022-01-06 13:13 UTC (permalink / raw)
To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: Thomas Markus <t.markus@proventis.net>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, January 6, 2022, Andreas Joseph Krogh <andreas@visena.com>
wrote:
>
>
> I think you misread my message. What I want is for the subscript-version:
>
> ('{"key":"value"}'::jsonb)['key']
>
> to return:
>
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value │
> └──────────┘
>
> instead of
>
> ┌─────────┐
> │ jsonb │
> ├─────────┤
> │ "value" │
> └─────────
>
>
A given syntax/operator can only return one thing so what you want is
fundamentally not possible.
David J.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
@ 2022-01-06 13:20 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:29 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Andreas Joseph Krogh @ 2022-01-06 13:20 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
På torsdag 06. januar 2022 kl. 14:13:40, skrev David G. Johnston <
david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>>: On Thursday,
January 6, 2022, Andreas Joseph Krogh <andreas@visena.com
<mailto:andreas@visena.com>> wrote:
I think you misread my message. What I want is for the subscript-version:
('{"key":"value"}'::jsonb)['key']
to return:
┌──────────┐
│ ?column? │
├──────────┤
│ value │
└──────────┘
instead of
┌─────────┐
│ jsonb │
├─────────┤
│ "value" │
└─────────
A given syntax/operator can only return one thing so what you want is
fundamentally not possible.
That's not very helpful....
Apparently I'm after a solution which either casts this to varchar or a
function that takes JSONB as argument and outputs the first field-value as
varchar.
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 13:29 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:33 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
0 siblings, 1 reply; 11+ messages in thread
From: David G. Johnston @ 2022-01-06 13:29 UTC (permalink / raw)
To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, January 6, 2022, Andreas Joseph Krogh <andreas@visena.com>
wrote:
>
> Apparently I'm after a solution which either casts this to varchar or a
> function that takes JSONB as argument and outputs the first field-value as
> varchar.
>
>
You can try casting the value though IIRC it doesn’t actually work
(limitation of the feature). The fact is that the ‘ - - > ‘ operator gives
you the needed output.
David J.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:29 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
@ 2022-01-06 13:33 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:42 ` Re: Getting json-value as varchar Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Andreas Joseph Krogh @ 2022-01-06 13:33 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
På torsdag 06. januar 2022 kl. 14:29:12, skrev David G. Johnston <
david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>>:
[..]
The fact is that the ‘ - - > ‘ operator gives you the needed output.
David J.
Yeah, I think that's the correct answer for this use-case.
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:29 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:33 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 13:42 ` Pavel Stehule <pavel.stehule@gmail.com>
2022-01-06 13:48 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
0 siblings, 1 reply; 11+ messages in thread
From: Pavel Stehule @ 2022-01-06 13:42 UTC (permalink / raw)
To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi
čt 6. 1. 2022 v 14:33 odesílatel Andreas Joseph Krogh <andreas@visena.com>
napsal:
> På torsdag 06. januar 2022 kl. 14:29:12, skrev David G. Johnston <
> david.g.johnston@gmail.com>:
>
> [..]
> The fact is that the ‘ - - > ‘ operator gives you the needed output.
>
> David J.
>
>
> Yeah, I think that's the correct answer for this use-case.
>
It is true that some other casting function is missing. I am not sure if
this is part of ANSI/SQL json support.
Now, you can use helper function
CREATE OR REPLACE FUNCTION public.to_text(jsonb)
RETURNS text
LANGUAGE sql
AS $function$
select jsonb_array_element_text($1, 0)
$function$
postgres=# select to_text((jsonb '{"a":"Ahoj"}')['a']);
┌─────────┐
│ to_text │
╞═════════╡
│ Ahoj │
└─────────┘
(1 row)
Regards
Pavel
>
> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com
> <https://www.visena.com;
>
>
Attachments:
noname (image/png, 1.9K) download
view image
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:29 ` Re: Getting json-value as varchar David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:33 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:42 ` Re: Getting json-value as varchar Pavel Stehule <pavel.stehule@gmail.com>
@ 2022-01-06 13:48 ` Andreas Joseph Krogh <andreas@visena.com>
0 siblings, 0 replies; 11+ messages in thread
From: Andreas Joseph Krogh @ 2022-01-06 13:48 UTC (permalink / raw)
To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
På torsdag 06. januar 2022 kl. 14:42:21, skrev Pavel Stehule <
pavel.stehule@gmail.com <mailto:pavel.stehule@gmail.com>>:
Hi
čt 6. 1. 2022 v 14:33 odesílatel Andreas Joseph Krogh <andreas@visena.com
<mailto:andreas@visena.com>> napsal:
På torsdag 06. januar 2022 kl. 14:29:12, skrev David G. Johnston <
david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>>:
[..]
The fact is that the ‘ - - > ‘ operator gives you the needed output.
David J.
Yeah, I think that's the correct answer for this use-case.
It is true that some other casting function is missing. I am not sure if this
is part of ANSI/SQL json support.
Now, you can use helper function
CREATE OR REPLACE FUNCTION public.to_text(jsonb)
RETURNS text
LANGUAGE sql
AS $function$
select jsonb_array_element_text($1, 0)
$function$
postgres=# select to_text((jsonb '{"a":"Ahoj"}')['a']);
┌─────────┐
│ to_text │
╞═════════╡
│ Ahoj │
└─────────┘
(1 row)
Regards
Pavel
Thanks!
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Re: Getting json-value as varchar Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Re: Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 13:25 ` Thomas Markus <t.markus@proventis.net>
1 sibling, 0 replies; 11+ messages in thread
From: Thomas Markus @ 2022-01-06 13:25 UTC (permalink / raw)
To: ; +Cc: pgsql-general@lists.postgresql.org
Hi,
Am 06.01.22 um 13:36 schrieb Andreas Joseph Krogh:
> På torsdag 06. januar 2022 kl. 13:31:19, skrev Thomas Markus
> <t.markus@proventis.net>:
>
> Hi,
> Am 06.01.22 um 13:28 schrieb Andreas Joseph Krogh:
>> Hi, in PG-14 this query returns "value" (with double-quotes):
>> SELECT ('{"key":"value"}'::jsonb)['key'];
>> ┌─────────┐
>> │ jsonb │
>> ├─────────┤
>> │ "value" │
>> └─────────┘
>> (1 row)
>>
>> and this returns 'value' (without the quotes):
>> SELECT ('{"key":"value"}'::jsonb)->> 'key';
>> ┌──────────┐
>> │ ?column? │
>> ├──────────┤
>> │ value │
>> └──────────┘
>> (1 row)
>>
>> How to I use the subscript syntax and get the result as varchar
>> instead of JSONB, assuming I /know/ the JSON-field is a String?
> simply cast your value
> SELECT (('{"key":"value"}'::jsonb)->> 'key')::text;
>
> best regards
> Thoma
>
> I think you misread my message. What I want is for the subscript-version:
>> ('{"key":"value"}'::jsonb)['key']
> to return:
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value │
> └──────────┘
>
> instead of
> ┌─────────┐
> │ jsonb │
> ├─────────┤
> │ "value" │
> └─────────┘
yeah right :(
complicated but this should do:
SELECT jsonb_build_array( ('{"key":"value"}'::jsonb)['key'] ) ->> 0;
> --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com <https://www.visena.com;
> <https://www.visena.com;
Attachments:
UCRjWK99lWlgoYmK.png (image/png, 1.9K) download
view image
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Getting json-value as varchar
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
@ 2022-01-06 14:39 ` Thomas Kellerer <shammat@gmx.net>
1 sibling, 0 replies; 11+ messages in thread
From: Thomas Kellerer @ 2022-01-06 14:39 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Andreas Joseph Krogh schrieb am 06.01.2022 um 13:28:
> Hi, in PG-14 this query returns "value" (with double-quotes):
> SELECT ('{"key":"value"}'::jsonb)['key'];
> ┌─────────┐
> │ jsonb │
> ├─────────┤
> │ "value" │
> └─────────┘
> (1 row)
>
> and this returns 'value' (without the quotes):
> SELECT ('{"key":"value"}'::jsonb)->> 'key';
> ┌──────────┐
> │ ?column? │
> ├──────────┤
> │ value │
> └──────────┘
> (1 row)
>
Unfortunately there isn't a direct cast from jsonb to text, but the #>> operator can be (mis)used for this:
SELECT ('{"key":"value"}'::jsonb)['key'] #>> '{}'
you can put that into a function if you need that frequently.
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2022-01-06 14:39 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-01-06 12:28 Getting json-value as varchar Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 12:31 ` Thomas Markus <t.markus@proventis.net>
2022-01-06 12:36 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:13 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:20 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:29 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-06 13:33 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:42 ` Pavel Stehule <pavel.stehule@gmail.com>
2022-01-06 13:48 ` Andreas Joseph Krogh <andreas@visena.com>
2022-01-06 13:25 ` Thomas Markus <t.markus@proventis.net>
2022-01-06 14:39 ` Thomas Kellerer <shammat@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox