JSON Object with variables into PostgreSQL












1















I have the following function:



create or replace function something(id integer)
RETURNS INTEGER
language plpgsql
as $$
DECLARE
name varchar;
email varchar;
route varchar;
now timestamp;
BEGIN
now = current_timestamp;
SELECT apd.apd_nome, apd.apd_email, CAST(r.rota_params->>'url_completa' as varchar) into name, email, route FROM avise_produto_disponivel apd
inner join produto p on apd.pro_id = p.pro_id
inner join produto_div div on p.pro_id = div.pro_id
inner join produto_oferta_div pod on div.pod_id = pod.pod_id
inner join produto_oferta po on po.ofe_id = pod.ofe_id
inner join rotas r on CAST(r.rota_params->>'oferta_id' as integer) = po.ofe_id
where apd.pro_id = $1;
DELETE FROM avise_produto_disponivel where pro_id = $1;
rota = CONCAT('https://www.something.com/', route);
INSERT INTO comunicacao (cot_id, com_para, com_assunto, com_data_criado, com_parametros, com_para_nome, cos_id, com_de) VALUES (1, email, 'Produto Chegou', now, '{"NOME": name, "URL": route}', name, 1, 'correio');
return 1;
END;
$$ ;


But when I try to execute I get the following error:



[22P02] ERROR: invalid input syntax for type json Detalhe: Token "nome" is invalid. Where: JSON data, line 1: {"NOME": name... PL/pgSQL function avise_me(integer) line 18 at SQL statement


Thanks in advance.










share|improve this question





























    1















    I have the following function:



    create or replace function something(id integer)
    RETURNS INTEGER
    language plpgsql
    as $$
    DECLARE
    name varchar;
    email varchar;
    route varchar;
    now timestamp;
    BEGIN
    now = current_timestamp;
    SELECT apd.apd_nome, apd.apd_email, CAST(r.rota_params->>'url_completa' as varchar) into name, email, route FROM avise_produto_disponivel apd
    inner join produto p on apd.pro_id = p.pro_id
    inner join produto_div div on p.pro_id = div.pro_id
    inner join produto_oferta_div pod on div.pod_id = pod.pod_id
    inner join produto_oferta po on po.ofe_id = pod.ofe_id
    inner join rotas r on CAST(r.rota_params->>'oferta_id' as integer) = po.ofe_id
    where apd.pro_id = $1;
    DELETE FROM avise_produto_disponivel where pro_id = $1;
    rota = CONCAT('https://www.something.com/', route);
    INSERT INTO comunicacao (cot_id, com_para, com_assunto, com_data_criado, com_parametros, com_para_nome, cos_id, com_de) VALUES (1, email, 'Produto Chegou', now, '{"NOME": name, "URL": route}', name, 1, 'correio');
    return 1;
    END;
    $$ ;


    But when I try to execute I get the following error:



    [22P02] ERROR: invalid input syntax for type json Detalhe: Token "nome" is invalid. Where: JSON data, line 1: {"NOME": name... PL/pgSQL function avise_me(integer) line 18 at SQL statement


    Thanks in advance.










    share|improve this question



























      1












      1








      1








      I have the following function:



      create or replace function something(id integer)
      RETURNS INTEGER
      language plpgsql
      as $$
      DECLARE
      name varchar;
      email varchar;
      route varchar;
      now timestamp;
      BEGIN
      now = current_timestamp;
      SELECT apd.apd_nome, apd.apd_email, CAST(r.rota_params->>'url_completa' as varchar) into name, email, route FROM avise_produto_disponivel apd
      inner join produto p on apd.pro_id = p.pro_id
      inner join produto_div div on p.pro_id = div.pro_id
      inner join produto_oferta_div pod on div.pod_id = pod.pod_id
      inner join produto_oferta po on po.ofe_id = pod.ofe_id
      inner join rotas r on CAST(r.rota_params->>'oferta_id' as integer) = po.ofe_id
      where apd.pro_id = $1;
      DELETE FROM avise_produto_disponivel where pro_id = $1;
      rota = CONCAT('https://www.something.com/', route);
      INSERT INTO comunicacao (cot_id, com_para, com_assunto, com_data_criado, com_parametros, com_para_nome, cos_id, com_de) VALUES (1, email, 'Produto Chegou', now, '{"NOME": name, "URL": route}', name, 1, 'correio');
      return 1;
      END;
      $$ ;


      But when I try to execute I get the following error:



      [22P02] ERROR: invalid input syntax for type json Detalhe: Token "nome" is invalid. Where: JSON data, line 1: {"NOME": name... PL/pgSQL function avise_me(integer) line 18 at SQL statement


      Thanks in advance.










      share|improve this question
















      I have the following function:



      create or replace function something(id integer)
      RETURNS INTEGER
      language plpgsql
      as $$
      DECLARE
      name varchar;
      email varchar;
      route varchar;
      now timestamp;
      BEGIN
      now = current_timestamp;
      SELECT apd.apd_nome, apd.apd_email, CAST(r.rota_params->>'url_completa' as varchar) into name, email, route FROM avise_produto_disponivel apd
      inner join produto p on apd.pro_id = p.pro_id
      inner join produto_div div on p.pro_id = div.pro_id
      inner join produto_oferta_div pod on div.pod_id = pod.pod_id
      inner join produto_oferta po on po.ofe_id = pod.ofe_id
      inner join rotas r on CAST(r.rota_params->>'oferta_id' as integer) = po.ofe_id
      where apd.pro_id = $1;
      DELETE FROM avise_produto_disponivel where pro_id = $1;
      rota = CONCAT('https://www.something.com/', route);
      INSERT INTO comunicacao (cot_id, com_para, com_assunto, com_data_criado, com_parametros, com_para_nome, cos_id, com_de) VALUES (1, email, 'Produto Chegou', now, '{"NOME": name, "URL": route}', name, 1, 'correio');
      return 1;
      END;
      $$ ;


      But when I try to execute I get the following error:



      [22P02] ERROR: invalid input syntax for type json Detalhe: Token "nome" is invalid. Where: JSON data, line 1: {"NOME": name... PL/pgSQL function avise_me(integer) line 18 at SQL statement


      Thanks in advance.







      json postgresql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 1:03









      vahdet

      2,06431434




      2,06431434










      asked Nov 22 '18 at 19:28









      Raphael AndresRaphael Andres

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          0














          you can test the usage of the JSON syntax by castin, test



             SELECT '{"NOME": "JOÃO", "URL": "http://rota.com.br"}'::json;


          It is a string representation transformed into JSON... And is not what you need.
          Try to use at INSERT INTO comunicacao something as



            json_build_object('nome', name,  'URL', route)


          See https://www.postgresql.org/docs/current/functions-json.html





          PS1: seems that there are also an error at the initial "SELECT INTO".



          PS2: I preefer to use JSONb instead JSON in near all application.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437088%2fjson-object-with-variables-into-postgresql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you can test the usage of the JSON syntax by castin, test



               SELECT '{"NOME": "JOÃO", "URL": "http://rota.com.br"}'::json;


            It is a string representation transformed into JSON... And is not what you need.
            Try to use at INSERT INTO comunicacao something as



              json_build_object('nome', name,  'URL', route)


            See https://www.postgresql.org/docs/current/functions-json.html





            PS1: seems that there are also an error at the initial "SELECT INTO".



            PS2: I preefer to use JSONb instead JSON in near all application.






            share|improve this answer




























              0














              you can test the usage of the JSON syntax by castin, test



                 SELECT '{"NOME": "JOÃO", "URL": "http://rota.com.br"}'::json;


              It is a string representation transformed into JSON... And is not what you need.
              Try to use at INSERT INTO comunicacao something as



                json_build_object('nome', name,  'URL', route)


              See https://www.postgresql.org/docs/current/functions-json.html





              PS1: seems that there are also an error at the initial "SELECT INTO".



              PS2: I preefer to use JSONb instead JSON in near all application.






              share|improve this answer


























                0












                0








                0







                you can test the usage of the JSON syntax by castin, test



                   SELECT '{"NOME": "JOÃO", "URL": "http://rota.com.br"}'::json;


                It is a string representation transformed into JSON... And is not what you need.
                Try to use at INSERT INTO comunicacao something as



                  json_build_object('nome', name,  'URL', route)


                See https://www.postgresql.org/docs/current/functions-json.html





                PS1: seems that there are also an error at the initial "SELECT INTO".



                PS2: I preefer to use JSONb instead JSON in near all application.






                share|improve this answer













                you can test the usage of the JSON syntax by castin, test



                   SELECT '{"NOME": "JOÃO", "URL": "http://rota.com.br"}'::json;


                It is a string representation transformed into JSON... And is not what you need.
                Try to use at INSERT INTO comunicacao something as



                  json_build_object('nome', name,  'URL', route)


                See https://www.postgresql.org/docs/current/functions-json.html





                PS1: seems that there are also an error at the initial "SELECT INTO".



                PS2: I preefer to use JSONb instead JSON in near all application.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 23:18









                Peter KraussPeter Krauss

                5,4731085174




                5,4731085174
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437088%2fjson-object-with-variables-into-postgresql%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy