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 ;
sqlite3 sqlite-json1
add a comment |
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 ;
sqlite3 sqlite-json1
add a comment |
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 ;
sqlite3 sqlite-json1
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
sqlite3 sqlite-json1
edited Nov 8 at 18:02
asked Nov 8 at 14:05
ingo
729
729
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
edited Nov 8 at 18:05
answered Nov 8 at 16:18
ingo
729
729
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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