How to loop through each key/value pair in a jsonb OBJECT in postgresql stored procedure?

Can an insurance company drop you after receiving a bill and refusing to pay?

My cat mixes up the floors in my building. How can I help him?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

Can I write a book of my D&D game?

Normalization for two bulk RNA-Seq samples to enable reliable fold-change estimation between genes

How long is the D&D Starter Set campaign?

Eww, those bytes are gross

How should I handle players who ignore the session zero agreement?

How can I get my players to come to the game session after agreeing to a date?

How to solve a large system of linear algebra?

Using only 1s, make 29 with the minimum number of digits

How do Chazal know that the descendants of a Mamzer may never marry into the general populace?

How to limit sight distance to 1 km

Why zero tolerance on nudity in space?

Avoiding morning and evening handshakes

Can a person refuse a presidential pardon?

Why publish a research paper when a blog post or a lecture slide can have more citation count than a journal paper?

Why isn't there a non-conducting core wire for high-frequency coil applications

Could a phylactery of a lich be a mirror or does it have to be a box?

Word or phrase for showing great skill at something WITHOUT formal training in it

Dilemma of explaining to interviewer that he is the reason for declining second interview

Do authors have to be politically correct in article-writing?

Would a National Army of mercenaries be a feasible idea?

Why avoid shared user accounts?



How to loop through each key/value pair in a jsonb OBJECT in postgresql stored procedure?














0















I am trying to create a function in postgres that can loop through each key/value pair in a given jsonb object.



create or replace function myFunction
(input jsonb)
returns jsonb as $$
BEGIN

// foreach(key in input)
// do some math operation on its corresponding value

returns input;

END; $$


The argument input is expected to be a jsonb OBJECT, such as {"a":1, "b":2, "c":3}.



So what I want to do, is to loop through each "key":value pair in the object. As you can tell from my comment, I used to write backend code with more general-purpose programming languages like c, java, etc. So I am not good at sql. I have tried to search online, but they talk about how to loop through jsonb array instead of object. So really stuck here.









share







New contributor




JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I am trying to create a function in postgres that can loop through each key/value pair in a given jsonb object.



    create or replace function myFunction
    (input jsonb)
    returns jsonb as $$
    BEGIN

    // foreach(key in input)
    // do some math operation on its corresponding value

    returns input;

    END; $$


    The argument input is expected to be a jsonb OBJECT, such as {"a":1, "b":2, "c":3}.



    So what I want to do, is to loop through each "key":value pair in the object. As you can tell from my comment, I used to write backend code with more general-purpose programming languages like c, java, etc. So I am not good at sql. I have tried to search online, but they talk about how to loop through jsonb array instead of object. So really stuck here.









    share







    New contributor




    JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I am trying to create a function in postgres that can loop through each key/value pair in a given jsonb object.



      create or replace function myFunction
      (input jsonb)
      returns jsonb as $$
      BEGIN

      // foreach(key in input)
      // do some math operation on its corresponding value

      returns input;

      END; $$


      The argument input is expected to be a jsonb OBJECT, such as {"a":1, "b":2, "c":3}.



      So what I want to do, is to loop through each "key":value pair in the object. As you can tell from my comment, I used to write backend code with more general-purpose programming languages like c, java, etc. So I am not good at sql. I have tried to search online, but they talk about how to loop through jsonb array instead of object. So really stuck here.









      share







      New contributor




      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am trying to create a function in postgres that can loop through each key/value pair in a given jsonb object.



      create or replace function myFunction
      (input jsonb)
      returns jsonb as $$
      BEGIN

      // foreach(key in input)
      // do some math operation on its corresponding value

      returns input;

      END; $$


      The argument input is expected to be a jsonb OBJECT, such as {"a":1, "b":2, "c":3}.



      So what I want to do, is to loop through each "key":value pair in the object. As you can tell from my comment, I used to write backend code with more general-purpose programming languages like c, java, etc. So I am not good at sql. I have tried to search online, but they talk about how to loop through jsonb array instead of object. So really stuck here.







      sql-server postgresql





      share







      New contributor




      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 34 secs ago









      JaneLJaneL

      1




      1




      New contributor




      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      JaneL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          JaneL is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231016%2fhow-to-loop-through-each-key-value-pair-in-a-jsonb-object-in-postgresql-stored-p%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          JaneL is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          JaneL is a new contributor. Be nice, and check out our Code of Conduct.













          JaneL is a new contributor. Be nice, and check out our Code of Conduct.












          JaneL is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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%2fdba.stackexchange.com%2fquestions%2f231016%2fhow-to-loop-through-each-key-value-pair-in-a-jsonb-object-in-postgresql-stored-p%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

          Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

          Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

          How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...