I am using MySQL 5.7+ with the native JSON data type.
Sample data:
set #jsn_string='{\"body\": {\"items\": \"[{\\"count\\":530,\\"id\\":5},{\\"count\\":1,\\"id\\":519},{\\"count\\":209,\\"id\\":522},{\\"count\\":0,\\"id\\":3004}]\"}}';
Questions:
i want to retrieve iterated key, value.
The following result has the position of the data
select json_extract(replace(replace(replace(trim(#jsn_string), '\"[', '['), ']\"', ']'), '\\"', '\"'),'$.body.items[0].id') as id,
json_extract(replace(replace(replace(trim(#jsn_string), '\"[', '['), ']\"', ']'), '\\"', '\"'),'$.body.items[0].count') as count;
result is
id count
5 530
but i want all of array, such as below
id[0] count[0],
id[1] count[1],
id[n] count[n]
Related
resp_data = array_to_json(array(
select jsonb_build_object(
'id', sp_authorities.db_id ,
'role_id', sp_authorities.role_id ,
'srvc_type_id', sp_authorities.srvc_type_id,
'form_data', sp_authorities.form_data ,
'sp_cust_fields_json',jsonb_agg(sp_orgs_settings.settings_data->'sp_cust_fields_json')
)
from public.cl_cf_srvc_prvdr_authorities as sp_authorities
left join public.cl_tx_orgs_settings as sp_orgs_settings
on sp_authorities.srvc_type_id = any(unnest(array(
SELECT json_array_elements_text(json_extract_path(sp_orgs_settings.settings_data,'srvc_type_id'::integer[])))
))
srvc_type_id array is not formed instead malformed array literal error is occuring
[enter image enter image description herehere](https://i.stack.imgur.com/2t1We.png)
To convert all the rows of a SELECT query into a single array, use the array_agg function.
SELECT array_agg(sp_authorities.srvc_type_id) ... -- Gives an array of raw integers
SELECT array_agg(json_build_object('id',sp_authorities.srvc_type_id)) ... -- Gives an array of json objects
SELECT array_to_json(array_agg ... -- Gives a json array of either of the above
I have a table that has the following data
START_DATE
NAME
ID
01/18/2022
JOHN
10
01/19/2022
ADAM
20
I am trying to convert this to JSON in a custom format like below -
{
"labels":{
"name":"JOHN",
"id":[10]
}
"values":{
"startDate":"01/18/2022"
}
}
PARSE_JSON way of
SELECT parse_json('{"values": {startDate: A.STARTDATE}}')
FROM TABLE_A A;
resulted in error
Error parsing JSON: unknown keyword "A", pos 25
OBJECT_CONSTRUCT results in converting column name as key and column value as value.
Please advise how to have custom field names in JSON conversion in Snowflake.
Renamed objects names as per data given and changed Id to array:
create table test1 values(START_DATE date, NAME string,ID number);
insert into test1(START_DATE, NAME,ID ) values('01/18/2022','JOHN', 10);
insert into test1(START_DATE, NAME,ID ) values('01/19/2022','ADAM', 20);
Select
OBJECT_CONSTRUCT('ID',id::array,'NAME',name) as label_obj,
OBJECT_CONSTRUCT(
'start_date',
START_DATE::string) as start_dt_obj,
object_insert(object_construct('labels', label_obj), 'values', start_dt_obj) as final_json
from
Test1;
One of my column response store the value like this in postgres table database:
{"result": [{"cin": "134234", "entityName": "xyz"}, {"cin": "2452352", "entityName": "uvw"}]
I want to return cin value.
I tried
SELECT response->'result'->'cin' AS Cin
FROM test where id = 1 ;
but it returned null;
Can anyone provide me a simple solution.
You can use a JSON path query for this:
select jsonb_path_query_first(response, '$.result[*] ? (#.entityName == "xyz").cin')
from test
where id = 1;
This assumes that response is a jsonb column (which it should be). If it's not you need to cast it response::jsonb
The result is a JSONB value again
Here is my JSON:
[{"Key":"schedulerItemType","Value":"schedule"},{"Key":"scheduleId","Value":"82"},{"Key":"scheduleEventId","Value":"-1"},{"Key":"scheduleTypeId","Value":"2"},{"Key":"scheduleName","Value":"Fixed Schedule"},{"Key":"moduleId","Value":"5"}]
I want to query the database by FileMetadata column
I've tried this:
SELECT * FROM FileSystemItems WHERE JSON_VALUE(FileMetadata, '$.Key') = 'scheduleId' and JSON_VALUE(FileMetadata, '$.Value') = '82'
but it doesn't work!
I had it working with just a dictionary key/value pair, but I needed to return the data differently, so I am adding it with key and value into the json now.
What am I doing wrong?
With the sample data given you'd have to supply an array index to query the 1th element (0-based array indexes), e.g.:
select *
from dbo.FileSystemItems
where json_value(FileMetadata, '$[1].Key') = 'scheduleId'
and json_value(FileMetadata, '$[1].Value') = '82'
If the scheduleId key can appear at arbitrary positions in the array then you can restructure the query to use OPENJSON instead, e.g.:
select *
from dbo.FileSystemItems
cross apply openjson(FileMetadata) with (
[Key] nvarchar(50) N'$.Key',
Value nvarchar(50) N'$.Value'
) j
where j.[Key] = N'scheduleId'
and j.Value = N'82'
I am processing an sqlite table which contains json objects. These json objects have keys that are empty strings. How can I retrieve the value? For example:
select json_extract('{"foo": "bar", "":"empty"}', '$.foo') as data;
-result: "bar"
How can I retrieve "empty"?
Using your example:
sqlite> SELECT value FROM json_each('{"foo":"bar","":"empty"}') WHERE key = '';
value
----------
empty
As part of a larger query from a table:
SELECT (SELECT j.value FROM json_each(t.your_json_column) AS j WHERE j.key = '') AS data
FROM your_table AS t;