Remove K,V pairs with value 'NULL' from json object in query result











up vote
0
down vote

favorite












The following gives me a result : {"a":null,"b":99.0,"c":null}
I'd like to have {"b":99.0} as a result so I can use the result in a JSON patch. How can I achieve this with sqlite/json1?



DROP TABLE IF EXISTS test;
CREATE TABLE test (
id INTEGER PRIMARY KEY,
a REAL, b REAL, c REAL
);

INSERT INTO test(a,b,c)
VALUES (1,2,3), (1,99,3);

SELECT json_object(
'a', NULLIF(new.a, curr.a),
'b', NULLIF(new.b, curr.b),
'c', NULLIF(new.c, curr.c)
) AS result
FROM test curr
INNER JOIN test new ON curr.id
WHERE new.id = 2 AND curr.id = new.id -1 ;









share|improve this question




























    up vote
    0
    down vote

    favorite












    The following gives me a result : {"a":null,"b":99.0,"c":null}
    I'd like to have {"b":99.0} as a result so I can use the result in a JSON patch. How can I achieve this with sqlite/json1?



    DROP TABLE IF EXISTS test;
    CREATE TABLE test (
    id INTEGER PRIMARY KEY,
    a REAL, b REAL, c REAL
    );

    INSERT INTO test(a,b,c)
    VALUES (1,2,3), (1,99,3);

    SELECT json_object(
    'a', NULLIF(new.a, curr.a),
    'b', NULLIF(new.b, curr.b),
    'c', NULLIF(new.c, curr.c)
    ) AS result
    FROM test curr
    INNER JOIN test new ON curr.id
    WHERE new.id = 2 AND curr.id = new.id -1 ;









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The following gives me a result : {"a":null,"b":99.0,"c":null}
      I'd like to have {"b":99.0} as a result so I can use the result in a JSON patch. How can I achieve this with sqlite/json1?



      DROP TABLE IF EXISTS test;
      CREATE TABLE test (
      id INTEGER PRIMARY KEY,
      a REAL, b REAL, c REAL
      );

      INSERT INTO test(a,b,c)
      VALUES (1,2,3), (1,99,3);

      SELECT json_object(
      'a', NULLIF(new.a, curr.a),
      'b', NULLIF(new.b, curr.b),
      'c', NULLIF(new.c, curr.c)
      ) AS result
      FROM test curr
      INNER JOIN test new ON curr.id
      WHERE new.id = 2 AND curr.id = new.id -1 ;









      share|improve this question















      The following gives me a result : {"a":null,"b":99.0,"c":null}
      I'd like to have {"b":99.0} as a result so I can use the result in a JSON patch. How can I achieve this with sqlite/json1?



      DROP TABLE IF EXISTS test;
      CREATE TABLE test (
      id INTEGER PRIMARY KEY,
      a REAL, b REAL, c REAL
      );

      INSERT INTO test(a,b,c)
      VALUES (1,2,3), (1,99,3);

      SELECT json_object(
      'a', NULLIF(new.a, curr.a),
      'b', NULLIF(new.b, curr.b),
      'c', NULLIF(new.c, curr.c)
      ) AS result
      FROM test curr
      INNER JOIN test new ON curr.id
      WHERE new.id = 2 AND curr.id = new.id -1 ;






      sqlite3 sqlite-json1






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 18:02

























      asked Nov 8 at 14:05









      ingo

      729




      729
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Some fiddling later:



          SELECT json_group_object(key, value) 
          FROM json_each(json('{"a":null, "b":99.0, "c":null}')) AS result
          WHERE result.value IS NOT NULL;


          results in {"b":99.0}



          So the whole thing becomes something like:



          DROP TABLE IF EXISTS test;
          CREATE TABLE test (
          id INTEGER PRIMARY KEY,
          a REAL,
          b REAL,
          c REAL
          );

          INSERT INTO test(a,b,c)
          VALUES (1,2,3), (1,99,3), (2,99,4), (1,999,3);

          WITH J(kv) AS (
          SELECT json_object(
          'a', NULLIF(new.a, curr.a),
          'b', NULLIF(new.b, curr.b),
          'c', NULLIF(new.c, curr.c)
          )
          FROM test curr
          INNER JOIN test new ON curr.id
          WHERE new.id = 2 AND curr.id = new.id -1
          )
          SELECT json_group_object(key, value) AS result
          FROM json_each((SELECT kv FROM J)) AS kv
          WHERE kv.value IS NOT NULL;





          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',
            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%2f53209382%2fremove-k-v-pairs-with-value-null-from-json-object-in-query-result%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








            up vote
            0
            down vote



            accepted










            Some fiddling later:



            SELECT json_group_object(key, value) 
            FROM json_each(json('{"a":null, "b":99.0, "c":null}')) AS result
            WHERE result.value IS NOT NULL;


            results in {"b":99.0}



            So the whole thing becomes something like:



            DROP TABLE IF EXISTS test;
            CREATE TABLE test (
            id INTEGER PRIMARY KEY,
            a REAL,
            b REAL,
            c REAL
            );

            INSERT INTO test(a,b,c)
            VALUES (1,2,3), (1,99,3), (2,99,4), (1,999,3);

            WITH J(kv) AS (
            SELECT json_object(
            'a', NULLIF(new.a, curr.a),
            'b', NULLIF(new.b, curr.b),
            'c', NULLIF(new.c, curr.c)
            )
            FROM test curr
            INNER JOIN test new ON curr.id
            WHERE new.id = 2 AND curr.id = new.id -1
            )
            SELECT json_group_object(key, value) AS result
            FROM json_each((SELECT kv FROM J)) AS kv
            WHERE kv.value IS NOT NULL;





            share|improve this answer



























              up vote
              0
              down vote



              accepted










              Some fiddling later:



              SELECT json_group_object(key, value) 
              FROM json_each(json('{"a":null, "b":99.0, "c":null}')) AS result
              WHERE result.value IS NOT NULL;


              results in {"b":99.0}



              So the whole thing becomes something like:



              DROP TABLE IF EXISTS test;
              CREATE TABLE test (
              id INTEGER PRIMARY KEY,
              a REAL,
              b REAL,
              c REAL
              );

              INSERT INTO test(a,b,c)
              VALUES (1,2,3), (1,99,3), (2,99,4), (1,999,3);

              WITH J(kv) AS (
              SELECT json_object(
              'a', NULLIF(new.a, curr.a),
              'b', NULLIF(new.b, curr.b),
              'c', NULLIF(new.c, curr.c)
              )
              FROM test curr
              INNER JOIN test new ON curr.id
              WHERE new.id = 2 AND curr.id = new.id -1
              )
              SELECT json_group_object(key, value) AS result
              FROM json_each((SELECT kv FROM J)) AS kv
              WHERE kv.value IS NOT NULL;





              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Some fiddling later:



                SELECT json_group_object(key, value) 
                FROM json_each(json('{"a":null, "b":99.0, "c":null}')) AS result
                WHERE result.value IS NOT NULL;


                results in {"b":99.0}



                So the whole thing becomes something like:



                DROP TABLE IF EXISTS test;
                CREATE TABLE test (
                id INTEGER PRIMARY KEY,
                a REAL,
                b REAL,
                c REAL
                );

                INSERT INTO test(a,b,c)
                VALUES (1,2,3), (1,99,3), (2,99,4), (1,999,3);

                WITH J(kv) AS (
                SELECT json_object(
                'a', NULLIF(new.a, curr.a),
                'b', NULLIF(new.b, curr.b),
                'c', NULLIF(new.c, curr.c)
                )
                FROM test curr
                INNER JOIN test new ON curr.id
                WHERE new.id = 2 AND curr.id = new.id -1
                )
                SELECT json_group_object(key, value) AS result
                FROM json_each((SELECT kv FROM J)) AS kv
                WHERE kv.value IS NOT NULL;





                share|improve this answer














                Some fiddling later:



                SELECT json_group_object(key, value) 
                FROM json_each(json('{"a":null, "b":99.0, "c":null}')) AS result
                WHERE result.value IS NOT NULL;


                results in {"b":99.0}



                So the whole thing becomes something like:



                DROP TABLE IF EXISTS test;
                CREATE TABLE test (
                id INTEGER PRIMARY KEY,
                a REAL,
                b REAL,
                c REAL
                );

                INSERT INTO test(a,b,c)
                VALUES (1,2,3), (1,99,3), (2,99,4), (1,999,3);

                WITH J(kv) AS (
                SELECT json_object(
                'a', NULLIF(new.a, curr.a),
                'b', NULLIF(new.b, curr.b),
                'c', NULLIF(new.c, curr.c)
                )
                FROM test curr
                INNER JOIN test new ON curr.id
                WHERE new.id = 2 AND curr.id = new.id -1
                )
                SELECT json_group_object(key, value) AS result
                FROM json_each((SELECT kv FROM J)) AS kv
                WHERE kv.value IS NOT NULL;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 18:05

























                answered Nov 8 at 16:18









                ingo

                729




                729






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53209382%2fremove-k-v-pairs-with-value-null-from-json-object-in-query-result%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()