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

279
Visualizações
load formatted json to postgreSQL table

I want to load a formatted JSON file, in the form of

{
"EId":"104111",
"Category":"(0)",
"Mac":"ABV",
"Path":"chemin2",
"ID":"System.Byte"
}

by creating first a temporary table with a json column,

 create temporary table temp_json (values json);
 copy temp_json from '/path_to_the_file/test.json';
 select values->>'EId' as EId,
       values->>'Category' as Category,
       values->>'Mac' as Mac,
       values->>'Path' as Path,
       values->>'ID' as ID      
  from(
          select json_array_elements(values) as values 
          from temp_json
  ) a;

but it shows the following message:

ERROR:  invalid input syntax for type JSON
DETAIL:  The input string ended unexpectedly.
CONTEXT:  JSON data, line 1: {
COPY temp_json, line 1, column values: "{"

once I erase all the whitespace, the instruction pass with no error.

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

0

Assuming a file like this:

{
"EId":"104111",
"Category":"(0)",
"Mac":"ABV",
"Path":"chemin2",
"ID":"System.Byte"
}
{
"EId":"104112",
"Category":"(1)",
"Mac":"CBV",
"Path":"chemin3",
"ID":"System.Byte"
}

The temporary table will receive text not json:

create temporary table temp_json (values text);
\copy temp_json from '/path_to/input.json';

The definitive table will have a json column:

create table t (obj jsonb);

Some string manipulation:

insert into t (obj)
select
    regexp_split_to_table(
        replace(v, $$"}{"$$, $$"}djue748wBc,l;09{"$$),
        'djue748wBc,l;09'
    )::jsonb
from (
    select string_agg(values, '') as v
    from temp_json
) s;
                                            obj                                             
--------------------------------------------------------------------------------------------
 {"ID": "System.Byte", "EId": "104111", "Mac": "ABV", "Path": "chemin2", "Category": "(0)"}
 {"ID": "System.Byte", "EId": "104112", "Mac": "CBV", "Path": "chemin3", "Category": "(1)"}
over 4 years ago · Santiago Trujillo Relatório

0

I don't think you're properly quoting this. See the docs on quoting, and on copy.

It's certainly possible,

CREATE TEMPORARY TABLE foo
AS
  SELECT $${
    "EId":"104111",
    "Category":"(0)",
    "Mac":"ABV",
    "Path":"chemin2",
    "ID":"System.Byte"
  }$$::jsonb AS jsondata;
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