Oracle Select from JSON column with hyphenated keys - json

Say I have the following stored in a JSON column in an Oracle database:
{
"key-with-hyphens": "value"
}
Assuming this column is called json_column in a table called my_table, then I'm trying to select the value for the "key-with-hyphens" key like so:
select json_column.key-with-hyphens from my_table;
This gives me the following error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
Is there some kind of special syntax you need to use when selecting the value for a hyphenated key from a JSON column in Oracle?

So it turns out that there are a couple ways you can do this.
Method 1:
Doing it the original way I was trying, there were a couple issues with this select:
select json_column.key-with-hyphens from my_table;
First off, key selection in JSON columns will not work unless you alias the table, as such:
select mt.json_column.key-with-hyphens from my_table mt;
However, this still doesn't fix the issue with the hyphenated key. To allow for hyphenated keys in this syntax, you need to wrap the name of the key in quotes:
select mt.json_column."key-with-hyphens" from my_table mt;
The above query will work as expected.
Method 2:
Another way you can do this, without aliasing the table, is using the json_value function in the select clause. You pass the JSON column as the first parameter, and the full path of the key you would like to select as the second parameter, using $ as the root context of the JSON object stored in your column. The following query should also work as expected:
select json_value(json_column, '$."key-with-hyphens"') from my_table;

Related

Search an array of objects for specific key having a certain value

I want to retrieve all rows from a table in MySQL table where there is a JSON column called items with content like this:
[{"code":"MH005","qte":1,"totalpriceItem":"28.00"},{"code":"MH027","qte":1,"totalpriceItem":"28.00"}]
[{"code":"MH027","qte":1,"totalpriceItem":"30.00"}]
[{"code":"MH024","qte":1,"totalpriceItem":"28.00"},{"code":"MH028","qte":1,"totalpriceItem":"28.00"},{"code":"MH027","qte":1,"totalpriceItem":"28.00"}]
[{"code":"MH028","qte":1,"totalpriceItem":"30.00"}]
Now i want to be able to select all the row where the key code has the value MH027.
I've tried this but without success:
SELECT *
FROM `transactions`
WHERE JSON_CONTAINS(`item`, 'MH027', '$[*].code')
Any help is much appreciated.
If I understand correctly you need to use JSON_CONTAINS as follows:
SELECT *
FROM transactions
WHERE JSON_CONTAINS(item, '{"code": "MH027"}', '$')
The second parameter must be valid JSON. You could use JSON_OBJECT('code', 'MH028') if the value is dynamic or could contain special characters.

Alter column varchar to json in psql

Hi i am trying to convert a column in my table from varchar to json and the table already had some string data. I tried doing that with the below command.
Database=# alter table table_name alter column message type json using
message::json;
But the command failed with the below error.
ERROR: invalid input syntax for type json
DETAIL: Token "This" is invalid.
CONTEXT: JSON data, line 1: This...
Note : The message column has a set of words with spaces like below.
"This is a message"
I am not sure what went wrong. Thanks in advance..
You can use to_jsonb() rather than casting:
alter table table_name
alter column message type jsonb using to_jsonb(message);
If you really want to use json (although jsonb is recommended), then cast the result back to a json type:
alter table table_name
alter column message type json using to_jsonb(message)::json;
But this seems rather strange for a column that doesn't contain "real" json values, only plain strings.
In my case, I wanna split varchar that have separator each n-character. For example
varchar "ab,cd,ef,gh" -> json ["ab","cd","ef","gh"]
First that I do, convert varchar into array with delimiter of comma (",")
ALTER table my_table ALTER column my_column TYPE text[] USING string_to_array(my_column,',')
Then, convert array of varchar into json (maybe will you use for GraphQL database)
ALTER table my_table ALTER column my_column TYPE json USING array_to_json(my_column)
That gives me a result of json(array) like ["ab","cd","ef","gh"]
You can simply run the following command below, it will surely convert the field type as well as the previous records, you might also need to update your models file of respective framework so you can work accordingly.
In my case, I was converting saves_states table's column named prev_month_access_counts type from text to json.
ALTER TABLE saved_states ALTER COLUMN prev_months_access_counts TYPE jsonb
using to_jsonb(prev_months_access_counts);

Count data in mysql json by key

I work in mysql 8 and have a problem on counting data in json format field. This is my table:
I want to count data in absensi field where the key is "657" and the value is "0". So, by this table it must give me result 4.
I tried to use JSON_EXTRACT(absensi, '$.657') but always give me some error [42000][3143] Invalid JSON path expression. The error is around character position 6.
Can you help me how to solved this problem?
Thank's in advance...
Your key value is a string. Treat it as string instead of integer.
select json_extract(absensi, '$."657"')
If you are using your field as key value, you can build the parameter using concat() function.
select json_extract(absensi, concat('$."', fieldA, '"')) from test;
see dbfiddle.

How to extract values from a numeric-keyed nested JSON field in MySQL

I have a MySQL table with a JSON column called sent. The entries in the column have information like below:
{
"data": {
"12":"1920293"
}
}
I'm trying to use the mysql query:
select sent->"$.data.12" from mytable
but I get an exception:
Invalid JSON path expression. The error is around character position 9.
Any idea How I can extract the information? The query works fine for non-numeric subfields.
#Ibrahim,
You have an error in your code. If you use number (or spaced words) as key in a JSON data type in MySQL, you'll need to double-quote it.
Therefore, the correct MySQL statement in your case is:
select sent->'$.data."12"' FROM mytable;
Thanks,
#JeffreyKilelo

get json key of postgres column containing a specific word

I'm trying to select a key from my db and set its value in a json column with postgres keys are finishing with "_alert".
So in my bd I have a column named data as a json and i just want the keys finishing with "_alert" like "ram_alert", "temperatures_alert", "disk_alert", "cpu_alert".
So I need to get the key and the value to compare with the data I have in my backend app to validate if I need to update the value or dont.
How to do this?
I get all the keys doing select json_object_keys(data) from devices but how to get the key/value pair.. is there a way to use the "like" expression here?
First off, note that your current query will only work if you have one tuple in your 'devices' table. Try inserting another row and you'll get:
ERROR: cannot call json_object_keys on an array
If you're certain that you're only ever going to have ONE result from this table, then the following query should give you what you want:
SELECT key,value FROM devices,json_each(devices.data) where key ~ '_alert$';
I'd still throw something like "LIMIT 1" onto your query to be safe.