How to get a json object with specific key values, from a json array column?












1















In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.



Example row:



[{bool:false, number:0, value:'hello'}, 
{bool:true, number:1, value:'world'},
{bool:true, number:2, value:'foo'},
{bool:false, number:1, value:'bar'}]


What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.



What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.



I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.



EDIT: If I do



SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');


I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.










share|improve this question





























    1















    In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.



    Example row:



    [{bool:false, number:0, value:'hello'}, 
    {bool:true, number:1, value:'world'},
    {bool:true, number:2, value:'foo'},
    {bool:false, number:1, value:'bar'}]


    What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.



    What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.



    I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.



    EDIT: If I do



    SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
    SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');


    I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.










    share|improve this question



























      1












      1








      1








      In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.



      Example row:



      [{bool:false, number:0, value:'hello'}, 
      {bool:true, number:1, value:'world'},
      {bool:true, number:2, value:'foo'},
      {bool:false, number:1, value:'bar'}]


      What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.



      What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.



      I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.



      EDIT: If I do



      SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
      SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');


      I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.










      share|improve this question
















      In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.



      Example row:



      [{bool:false, number:0, value:'hello'}, 
      {bool:true, number:1, value:'world'},
      {bool:true, number:2, value:'foo'},
      {bool:false, number:1, value:'bar'}]


      What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.



      What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.



      I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.



      EDIT: If I do



      SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
      SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');


      I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.







      mysql arrays json object






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 19:49







      Jordan Youngs

















      asked Jan 18 at 19:38









      Jordan YoungsJordan Youngs

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use JSON_TABLE to convert the JSON into a derived table which you can then extract values from:



          SELECT j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          value
          world


          If you also want to get the index within each JSON value of the value which matched, you can add a FOR ORDINALITY clause to your JSON_TABLE e.g.:



          SELECT j.idx, j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(idx FOR ORDINALITY,
          bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          idx     value
          2 world


          Demo on dbfiddle






          share|improve this answer


























          • Thank you! This worked and your examples were very helpful

            – Jordan Youngs
            Jan 21 at 16:00











          • @JordanYoungs no worries. Glad I could help.

            – Nick
            Jan 21 at 21:18











          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%2f54260416%2fhow-to-get-a-json-object-with-specific-key-values-from-a-json-array-column%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 use JSON_TABLE to convert the JSON into a derived table which you can then extract values from:



          SELECT j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          value
          world


          If you also want to get the index within each JSON value of the value which matched, you can add a FOR ORDINALITY clause to your JSON_TABLE e.g.:



          SELECT j.idx, j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(idx FOR ORDINALITY,
          bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          idx     value
          2 world


          Demo on dbfiddle






          share|improve this answer


























          • Thank you! This worked and your examples were very helpful

            – Jordan Youngs
            Jan 21 at 16:00











          • @JordanYoungs no worries. Glad I could help.

            – Nick
            Jan 21 at 21:18
















          0














          You can use JSON_TABLE to convert the JSON into a derived table which you can then extract values from:



          SELECT j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          value
          world


          If you also want to get the index within each JSON value of the value which matched, you can add a FOR ORDINALITY clause to your JSON_TABLE e.g.:



          SELECT j.idx, j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(idx FOR ORDINALITY,
          bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          idx     value
          2 world


          Demo on dbfiddle






          share|improve this answer


























          • Thank you! This worked and your examples were very helpful

            – Jordan Youngs
            Jan 21 at 16:00











          • @JordanYoungs no worries. Glad I could help.

            – Nick
            Jan 21 at 21:18














          0












          0








          0







          You can use JSON_TABLE to convert the JSON into a derived table which you can then extract values from:



          SELECT j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          value
          world


          If you also want to get the index within each JSON value of the value which matched, you can add a FOR ORDINALITY clause to your JSON_TABLE e.g.:



          SELECT j.idx, j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(idx FOR ORDINALITY,
          bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          idx     value
          2 world


          Demo on dbfiddle






          share|improve this answer















          You can use JSON_TABLE to convert the JSON into a derived table which you can then extract values from:



          SELECT j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          value
          world


          If you also want to get the index within each JSON value of the value which matched, you can add a FOR ORDINALITY clause to your JSON_TABLE e.g.:



          SELECT j.idx, j.value
          FROM test t
          JOIN JSON_TABLE(t.jsonStr,
          '$[*]'
          COLUMNS(idx FOR ORDINALITY,
          bool BOOLEAN PATH '$.bool',
          number INT PATH '$.number',
          value VARCHAR(20) PATH '$.value')) j
          WHERE j.bool = true AND j.number = 1


          Output:



          idx     value
          2 world


          Demo on dbfiddle







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 18 at 23:23

























          answered Jan 18 at 20:20









          NickNick

          27.1k111940




          27.1k111940













          • Thank you! This worked and your examples were very helpful

            – Jordan Youngs
            Jan 21 at 16:00











          • @JordanYoungs no worries. Glad I could help.

            – Nick
            Jan 21 at 21:18



















          • Thank you! This worked and your examples were very helpful

            – Jordan Youngs
            Jan 21 at 16:00











          • @JordanYoungs no worries. Glad I could help.

            – Nick
            Jan 21 at 21:18

















          Thank you! This worked and your examples were very helpful

          – Jordan Youngs
          Jan 21 at 16:00





          Thank you! This worked and your examples were very helpful

          – Jordan Youngs
          Jan 21 at 16:00













          @JordanYoungs no worries. Glad I could help.

          – Nick
          Jan 21 at 21:18





          @JordanYoungs no worries. Glad I could help.

          – Nick
          Jan 21 at 21:18


















          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%2f54260416%2fhow-to-get-a-json-object-with-specific-key-values-from-a-json-array-column%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage