Sqlite.net JSON extension syntax












0















I'm trying to understand how the Sqlite JSON extensions work. Lets say a company has several branches and each branch has a catalogue of stock, so I create a table like:-



SQL = "CREATE TABLE BranchStock (branch VARCHAR, stock JSON)"
....


I then insert a few items like:-



SQL = "INSERT INTO BranchStock (branch, stock) values('Melbourne', json_array(json_object('catnumber', 'ABC123', 'instock',2), json_object('catnumber', 'BCD321', 'instock',5)))"
....

SQL = "INSERT INTO BranchStock (branch, stock) values('Sydney', json_array(json_object('catnumber', 'ABC123', 'instock',5), json_object('catnumber', 'XYZ567', 'instock',3)))"
....


Now I'm looking for the search sql to list which branches have the catnumber='ABC123', and the instock amounts. Perhaps something like this:-



SQL = "SELECT branch, json_extract(stock, '$.instock') FROM BranchStock WHERE json_valid(stock) AND json_extract(stock, '$.catnumber') = 'ABC123'


Can someone help me with the correct SELECT syntax?



Thanks in advance










share|improve this question





























    0















    I'm trying to understand how the Sqlite JSON extensions work. Lets say a company has several branches and each branch has a catalogue of stock, so I create a table like:-



    SQL = "CREATE TABLE BranchStock (branch VARCHAR, stock JSON)"
    ....


    I then insert a few items like:-



    SQL = "INSERT INTO BranchStock (branch, stock) values('Melbourne', json_array(json_object('catnumber', 'ABC123', 'instock',2), json_object('catnumber', 'BCD321', 'instock',5)))"
    ....

    SQL = "INSERT INTO BranchStock (branch, stock) values('Sydney', json_array(json_object('catnumber', 'ABC123', 'instock',5), json_object('catnumber', 'XYZ567', 'instock',3)))"
    ....


    Now I'm looking for the search sql to list which branches have the catnumber='ABC123', and the instock amounts. Perhaps something like this:-



    SQL = "SELECT branch, json_extract(stock, '$.instock') FROM BranchStock WHERE json_valid(stock) AND json_extract(stock, '$.catnumber') = 'ABC123'


    Can someone help me with the correct SELECT syntax?



    Thanks in advance










    share|improve this question



























      0












      0








      0








      I'm trying to understand how the Sqlite JSON extensions work. Lets say a company has several branches and each branch has a catalogue of stock, so I create a table like:-



      SQL = "CREATE TABLE BranchStock (branch VARCHAR, stock JSON)"
      ....


      I then insert a few items like:-



      SQL = "INSERT INTO BranchStock (branch, stock) values('Melbourne', json_array(json_object('catnumber', 'ABC123', 'instock',2), json_object('catnumber', 'BCD321', 'instock',5)))"
      ....

      SQL = "INSERT INTO BranchStock (branch, stock) values('Sydney', json_array(json_object('catnumber', 'ABC123', 'instock',5), json_object('catnumber', 'XYZ567', 'instock',3)))"
      ....


      Now I'm looking for the search sql to list which branches have the catnumber='ABC123', and the instock amounts. Perhaps something like this:-



      SQL = "SELECT branch, json_extract(stock, '$.instock') FROM BranchStock WHERE json_valid(stock) AND json_extract(stock, '$.catnumber') = 'ABC123'


      Can someone help me with the correct SELECT syntax?



      Thanks in advance










      share|improve this question
















      I'm trying to understand how the Sqlite JSON extensions work. Lets say a company has several branches and each branch has a catalogue of stock, so I create a table like:-



      SQL = "CREATE TABLE BranchStock (branch VARCHAR, stock JSON)"
      ....


      I then insert a few items like:-



      SQL = "INSERT INTO BranchStock (branch, stock) values('Melbourne', json_array(json_object('catnumber', 'ABC123', 'instock',2), json_object('catnumber', 'BCD321', 'instock',5)))"
      ....

      SQL = "INSERT INTO BranchStock (branch, stock) values('Sydney', json_array(json_object('catnumber', 'ABC123', 'instock',5), json_object('catnumber', 'XYZ567', 'instock',3)))"
      ....


      Now I'm looking for the search sql to list which branches have the catnumber='ABC123', and the instock amounts. Perhaps something like this:-



      SQL = "SELECT branch, json_extract(stock, '$.instock') FROM BranchStock WHERE json_valid(stock) AND json_extract(stock, '$.catnumber') = 'ABC123'


      Can someone help me with the correct SELECT syntax?



      Thanks in advance







      json sqlite sqlite3 sqlite-net






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 0:52









      dbc

      53.8k869122




      53.8k869122










      asked Nov 13 '18 at 23:50









      42LeapsOfFaith42LeapsOfFaith

      515




      515
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Note: I don't recommend actually using this for anything that's going to be run frequently or on huge tables, but if you absolutely have to...



          Given the following test table:



          CREATE TABLE BranchStock(branch TEXT, stock TEXT);
          INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":2},{"catnumber":"BCD321","instock":5}]');
          INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');


          Getting the branches and instock number for stores with 'ABC123':



          SELECT branch, json_extract(value, '$.instock') AS instock
          FROM BranchStock JOIN json_each(stock)
          WHERE json_extract(value, '$.catnumber') = 'ABC123';


          gives



          branch      instock   
          ---------- ----------
          Melbourne 2
          Sydney 5


          It'd be a lot easier and more efficient to do pretty much anything using a table definition like



          CREATE TABLE BranchStock(branch TEXT, catid TEXT, instock INTEGER);


          instead of trying to search through JSON data stored in a column. For example:



          SELECT branch, instock FROM BranchStock WHERE catid = 'ABC123';


          especially when you start adding indexes into the mix.






          share|improve this answer


























          • Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

            – 42LeapsOfFaith
            Nov 14 '18 at 8: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%2f53291149%2fsqlite-net-json-extension-syntax%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














          Note: I don't recommend actually using this for anything that's going to be run frequently or on huge tables, but if you absolutely have to...



          Given the following test table:



          CREATE TABLE BranchStock(branch TEXT, stock TEXT);
          INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":2},{"catnumber":"BCD321","instock":5}]');
          INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');


          Getting the branches and instock number for stores with 'ABC123':



          SELECT branch, json_extract(value, '$.instock') AS instock
          FROM BranchStock JOIN json_each(stock)
          WHERE json_extract(value, '$.catnumber') = 'ABC123';


          gives



          branch      instock   
          ---------- ----------
          Melbourne 2
          Sydney 5


          It'd be a lot easier and more efficient to do pretty much anything using a table definition like



          CREATE TABLE BranchStock(branch TEXT, catid TEXT, instock INTEGER);


          instead of trying to search through JSON data stored in a column. For example:



          SELECT branch, instock FROM BranchStock WHERE catid = 'ABC123';


          especially when you start adding indexes into the mix.






          share|improve this answer


























          • Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

            – 42LeapsOfFaith
            Nov 14 '18 at 8:18


















          0














          Note: I don't recommend actually using this for anything that's going to be run frequently or on huge tables, but if you absolutely have to...



          Given the following test table:



          CREATE TABLE BranchStock(branch TEXT, stock TEXT);
          INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":2},{"catnumber":"BCD321","instock":5}]');
          INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');


          Getting the branches and instock number for stores with 'ABC123':



          SELECT branch, json_extract(value, '$.instock') AS instock
          FROM BranchStock JOIN json_each(stock)
          WHERE json_extract(value, '$.catnumber') = 'ABC123';


          gives



          branch      instock   
          ---------- ----------
          Melbourne 2
          Sydney 5


          It'd be a lot easier and more efficient to do pretty much anything using a table definition like



          CREATE TABLE BranchStock(branch TEXT, catid TEXT, instock INTEGER);


          instead of trying to search through JSON data stored in a column. For example:



          SELECT branch, instock FROM BranchStock WHERE catid = 'ABC123';


          especially when you start adding indexes into the mix.






          share|improve this answer


























          • Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

            – 42LeapsOfFaith
            Nov 14 '18 at 8:18
















          0












          0








          0







          Note: I don't recommend actually using this for anything that's going to be run frequently or on huge tables, but if you absolutely have to...



          Given the following test table:



          CREATE TABLE BranchStock(branch TEXT, stock TEXT);
          INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":2},{"catnumber":"BCD321","instock":5}]');
          INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');


          Getting the branches and instock number for stores with 'ABC123':



          SELECT branch, json_extract(value, '$.instock') AS instock
          FROM BranchStock JOIN json_each(stock)
          WHERE json_extract(value, '$.catnumber') = 'ABC123';


          gives



          branch      instock   
          ---------- ----------
          Melbourne 2
          Sydney 5


          It'd be a lot easier and more efficient to do pretty much anything using a table definition like



          CREATE TABLE BranchStock(branch TEXT, catid TEXT, instock INTEGER);


          instead of trying to search through JSON data stored in a column. For example:



          SELECT branch, instock FROM BranchStock WHERE catid = 'ABC123';


          especially when you start adding indexes into the mix.






          share|improve this answer















          Note: I don't recommend actually using this for anything that's going to be run frequently or on huge tables, but if you absolutely have to...



          Given the following test table:



          CREATE TABLE BranchStock(branch TEXT, stock TEXT);
          INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":2},{"catnumber":"BCD321","instock":5}]');
          INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');


          Getting the branches and instock number for stores with 'ABC123':



          SELECT branch, json_extract(value, '$.instock') AS instock
          FROM BranchStock JOIN json_each(stock)
          WHERE json_extract(value, '$.catnumber') = 'ABC123';


          gives



          branch      instock   
          ---------- ----------
          Melbourne 2
          Sydney 5


          It'd be a lot easier and more efficient to do pretty much anything using a table definition like



          CREATE TABLE BranchStock(branch TEXT, catid TEXT, instock INTEGER);


          instead of trying to search through JSON data stored in a column. For example:



          SELECT branch, instock FROM BranchStock WHERE catid = 'ABC123';


          especially when you start adding indexes into the mix.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 4:32

























          answered Nov 14 '18 at 4:20









          ShawnShawn

          3,6781613




          3,6781613













          • Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

            – 42LeapsOfFaith
            Nov 14 '18 at 8:18





















          • Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

            – 42LeapsOfFaith
            Nov 14 '18 at 8:18



















          Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

          – 42LeapsOfFaith
          Nov 14 '18 at 8:18







          Thank you very much, it's exactly what I was looking for. I take your point that it is not the solution for performance - but it does offer a simple db design... Thanks again.

          – 42LeapsOfFaith
          Nov 14 '18 at 8: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%2f53291149%2fsqlite-net-json-extension-syntax%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







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud