Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

64
Visualizações
Selecting data within JSON

I'd like to pull data using SQL in Postgres but would like to go into the JSON result and only extract the data I need.

If I write (in Valentina studio):

Select "data" from "cars" 

The first row looks like:

[{"Model": "Golf", "Make": "VW", "Engine": "2.9"}, 
{"Model": "M3", "Make": "BMW", "Engine": "3.0"}]

What I would like is simply:

Golf, M3 or "Golf", "M3"

Then I could also use the same method for "Make" or "Engine"

Essentially I don't want my results in JSON.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use json_array_elements():

with cars(data) as (
values
    ('[
        {"Model": "Golf", "Make": "VW", "Engine": "2.9"}, 
        {"Model": "M3", "Make": "BMW", "Engine": "3.0"}
    ]'::json)
)

select 
    elem->>'Model' as model,
    elem->>'Make' as make,
    elem->>'Engine' as engine
from cars,
lateral json_array_elements(data) elem

 model | make | engine 
-------+------+--------
 Golf  | VW   | 2.9
 M3    | BMW  | 3.0
(2 rows)    
over 4 years ago · Santiago Trujillo Relatório

0

SELECT string_agg(x->>'Model', ',')
FROM cars
   CROSS JOIN LATERAL
      jsonb_array_elements(data) x
GROUP BY cars;

┌────────────┐
│ string_agg │
├────────────┤
│ Golf,M3    │
└────────────┘
(1 row)
over 4 years ago · Santiago Trujillo Relatório

0

I think I also got what I wanted (and melted it the way I was planning to do) with:

    select obj->'Model' as model, obj->'Make' as make, 
obj->'Engine' as engine from data n, jsonb_array_elements(n.DATA) as obj 

from the above I can also use:

select 
    elem->>'Model' as model,
    elem->>'Make' as make ,
    elem->>'Engine' as engine
from cars,
lateral jsonb_array_elements(data) elem;

Thanks for the help!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda