Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

832
Vistas
Postgresql string to jsonb_each_text()

I have this string (character varying) as a row value in Postgresql :

{'img_0': 'https://random.com/xxxxxx.jpg', 'img_1': 'https://random.com/yyyyyy.jpg', 'img_2': 'https://random.com/zzzzzz.jpg'}

I am trying to json_eact_text() it but can't figure out how to. I have tried to_jsonb() on it (working) and then jsonb_each(), but I have this error :

ERROR:  cannot call jsonb_each on a non-object

My query :

WITH 
test AS (
    SELECT to_jsonb(value) as value FROM attribute_value WHERE id = 43918
)

SELECT jsonb_each(value) FROM test
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your text value is not valid JSON. JSON requires doublequotes (") to delimit strings.

This will work by doctoring your text provided that your data is consistently wrong:

with t (sometext) as (
  values ($${'img_0': 'https://random.com/xxxxxx.jpg', 'img_1': 'https://random.com/yyyyyy.jpg', 'img_2': 'https://random.com/zzzzzz.jpg'}$$)
)
select jsonb_each_text(replace(sometext, '''', '"')::jsonb)
  from t;

            jsonb_each_text            
---------------------------------------
 (img_0,https://random.com/xxxxxx.jpg)
 (img_1,https://random.com/yyyyyy.jpg)
 (img_2,https://random.com/zzzzzz.jpg)
(3 rows)

To break this out into columns:

with t (sometext) as (
  values ($${'img_0': 'https://random.com/xxxxxx.jpg', 'img_1': 'https://random.com/yyyyyy.jpg', 'img_2': 'https://random.com/zzzzzz.jpg'}$$)
)
select j.*
  from t
 cross join lateral jsonb_each_text(replace(sometext, '''', '"')::jsonb) as j;

  key  |             value             
-------+-------------------------------
 img_0 | https://random.com/xxxxxx.jpg
 img_1 | https://random.com/yyyyyy.jpg
 img_2 | https://random.com/zzzzzz.jpg
(3 rows)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda