Select the Columns of table that belong in a JSON - mysql

I have a json like:
'attributes':[
{'attribute_name': 'name1','weight' = 10},
{'attribute_name': 'name2','weight' = 5},
...
]
}
Assuming all the attribute_name in the JSON are columns in a table. How to select only those columns from a table

Related

Conditionally select values from an array in a nested JSON string in a Mysql database

I am struggling to conditionally extract values from a nested JSON string in the Mysql table.
{"users": [{"userId": "10000001", "userToken": "11000000000001", "userTokenValidity": 1}, {"userId": "10000002", "userToken": "12000000000001", "userTokenValidity": 1}, {"userId": "10000003", "userToken": "13000000000001", "userTokenValidity": 0}]}
I want to select a userToken but only if the userTokenValidity is 1. So in this example only "11000000000001" and "12000000000001" should get selected.
This will extract the whole array ... how should I filter the result?
SELECT t.my_column->>"$.users" FROM my_table t;
SELECT CAST(value AS CHAR) output
FROM test
CROSS JOIN JSON_TABLE(test.data, '$.users[*]' COLUMNS (value JSON PATH '$')) jsontable
WHERE value->>'$.userTokenValidity' = 1
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4876ec22a9df4f6d2e75a476a02a2615

How to select data from jsonb column?

I have got one column with type jsonb. The data in this column looks as below
{
"random_number1":
{
"random_number2":
{
"Param1": 2,
"Param2": 0,
"Param3": 0,
"Param4": 6,
"Param5": 3
}
}
}
How to write select for this column if I want f.e. all rows where "Param3"=6 ?
I tried something like that
SELECT * FROM table WHERE column->'Param3' #> '6'::jsonb;
It depends on your expectations.
Get the value of a specified path:
select *
from my_table
where my_col->'random_number1'->'random_number2'->>'Param3' = '6'
Get the value of the key Param3 of any object on the third level:
select t.*
from my_table t,
jsonb_each(my_col) as value1(key1, value1),
jsonb_each(value1) as value2(key2, value2)
where jsonb_typeof(my_col) = 'object'
and jsonb_typeof(value1) = 'object'
and value2->>'Param3' = '6';
In the second case you may want to use distinct as the query may yield duplicated rows.

How to get the number of elements in a JSON array stored as CLOB with Oracle 12c?

I'm storing a java class A as A_DOC in a clob column in my database.
The structure of A is like:
{
id : 123
var1: abc
subvalues : [{
id: 1
value : a
},
{
id: 1
value :b
}
...
}
]}
I know I can do things like
select json_query(a.A_DOC, '$.subvalues.value') from table_name a;
and so on, but how I'm looking for a way to count the number of elements in the subvalues array through an sql query. Is this possible?
the function exists in Oracle 18 only
SELECT json_query('[19, 15, [16,2,3]]','$[*].size()' WITH ARRAY WRAPPER) FROM dual;
SELECT json_value('[19, 15, [16,2,3]]','$.size()') FROM dual;
You can use JSON_TABLE:
SELECT
id, var1, count(sub_id) subvalues
FROM
JSON_TABLE (
to_clob('{ id: 123, var1: "abc", subvalues : [{ id: 1, value: "a", }, { id: 2, value: "b" } ]}'),
'$'
COLUMNS (
id NUMBER PATH '$.id',
var1 VARCHAR PATH '$.var1',
NESTED PATH '$.subvalues[*]'
COLUMNS (
sub_id NUMBER PATH '$.id'
)
)
)
GROUP BY id, var1

Does Postgres allow json[] or jsonb[]?

So l have been trying to find an answer on the internet with 0 luck.
Does postgres support having arrays of objects in a single field, e.g.
[
{
key: value,
another: value
},
{
key: value,
value: key
}
]
and saving this to a single field?
Also how would you perform the single INSERT or UPDATE
would it be: UPDATE db SET value='[{ key: val }, { key: val }]' ??
Postgres supports any valid json values, including json arrays.
What you are going to use is a single json (jsonb) column, not a Postgres array:
create table example (id int, val jsonb);
insert into example
values (1, '[{ "name": "aga" }, { "gender": "female" }]');
select * from example;
id | val
----+-----------------------------------------
1 | [{"name": "aga"}, {"gender": "female"}]
(1 row)
It depends on your definitoion of objects I guess.
You can use JSON: http://www.postgresql.org/docs/current/static/functions-json.html and insert unstructured data:
# create table test (field json);
CREATE TABLE
# insert into test values ('[1,2,3]');
INSERT 0 1
# insert into test values ('[{"key": "value"}, {"key": "value"}]');
INSERT 0 1
# select * from test;
field
--------------------------------------
[1,2,3]
[{"key": "value"}, {"key": "value"}]
There is also support for arrays: http://www.postgresql.org/docs/current/static/arrays.html

Sort by length of nested JSON array

Let's assume I have a PostgreSQL table with following schema:
id: 1,
attribute_a: 'value'
attribute_b: 'value'
data: { attribute_c: 'value', array_of_values: [1,2,3] }
Where data is stored in a JSON structure. Is that possible to order the elements in the table by array_of_values length?
Use json_array_length() or jsonb_array_length() like Eggplant commented. Assuming jsonb:
SELECT *
FROM tbl
ORDER BY jsonb_array_length(data -> 'array_of_values')
BTW, the syntax for your JSON value should be:
{"attribute_c": "value", "array_of_values": [1, 2, 3]}