pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #11021: How to extract text value from json scalar?
2+ messages / 2 participants
[nested] [flat]

* BUG #11021: How to extract text value from json scalar?
@ 2014-07-22 11:02 kolmyk@hotmail.com
  2014-07-23 00:12 ` Re: BUG #11021: How to extract text value from json scalar? Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: kolmyk@hotmail.com @ 2014-07-22 11:02 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      11021
Logged by:          Alexey
Email address:      kolmyk@hotmail.com
PostgreSQL version: 9.3.4
Operating system:   Windows 8.0
Description:        

I'm using json_each function for iterate keys and values in my function, but
string values contains quotes.
I can use json_each_text, but sometimes I need to json value.
Can I extract text value from json scalar?

Now I'm using this alternative script:
('[' || value ||']')::json->>0

Example:
select key, value, ('[' || value ||']')::json->>0 from
json_each('{"a":123,"b":"first"}');



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #11021: How to extract text value from json scalar?
  2014-07-22 11:02 BUG #11021: How to extract text value from json scalar? kolmyk@hotmail.com
@ 2014-07-23 00:12 ` Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Michael Paquier @ 2014-07-23 00:12 UTC (permalink / raw)
  To: kolmyk@hotmail.com; +Cc: pgsql-bugs

On Tue, Jul 22, 2014 at 8:02 PM,  <kolmyk@hotmail.com> wrote:
> I'm using json_each function for iterate keys and values in my function, but
> string values contains quotes.
> I can use json_each_text, but sometimes I need to json value.
> Can I extract text value from json scalar?
Not that I am aware of. You could always compare the results of
json_each and json_each_text and grab the fields that have as only
differences those quotes, but this would be more costly than the trick
you are mentioning below. Also, a text without quotes is not valid in
json AFAIK, that's why you cannot cast a value obtained with
json_each_text back to json. If I'm wrong, anyone, feel free to
correct though.

With your solution, nested json fields are however in correct shape:
=# SELECT key, value, ('[' || value ||']')::json->>0 as col3
     FROM json_each('{"a":123,"b":"first","c":{"d":1,"e":"text"}}');
 key |       value        |        col3
-----+--------------------+--------------------
 a   | 123                | 123
 b   | "first"            | first
 c   | {"d":1,"e":"text"} | {"d":1,"e":"text"}
(3 rows)
You could always do some extra operations on the fields obtained, or
catch casting errors if your function is for example in plpgsql.

> Now I'm using this alternative script:
> ('[' || value ||']')::json->>0
>
> Example:
> select key, value, ('[' || value ||']')::json->>0 from
> json_each('{"a":123,"b":"first"}');
By the way, this is not a bug, and the mailing list pgsql-general is
more appropriate.
Regards,
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2014-07-23 00:12 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-07-22 11:02 BUG #11021: How to extract text value from json scalar? kolmyk@hotmail.com
2014-07-23 00:12 ` Michael Paquier <michael.paquier@gmail.com>

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