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.
Related
I have a column in MYSQL table from which I want to get object's first value, and I don't know what is the kay name. Values are like:
{"5":"ABC"}
{"8":"CDE"}
I have tried JSON_EXTRACT, JSON_KEYS but nothing work for me.
Below query worked for me:
SELECT json_extract(json_data,'$[0].*') from table
For detailed documentation, go through JSON_EXTRACT once.
I'm trying to check if key-value exist in a JSON document in mysql. This is the query I use:
SELECT (JSON_EXTRACT(saved_stuff, '$.key') IS NOT NULL) FROM table
Now when the key is "test" it works great. It returns either 0 or 1, depending on whether the document exists. When the key is "34f8d790-6e25-4f31-ae8a-a17c04c96045" I get the error:
"Error: Query failed: ER_INVALID_JSON_PATH: Invalid JSON part expression. The error is around character position 38"
I'm thinking it has something to do with the length of the key, but I'm not sure. Please, any help is greatly appreciated.
See 11.5 The JSON Data Type :: Searching and Modifying JSON Values:
...
... The key name must be specified within double quotation marks
if the name without quotes is not legal within path expressions (for
example, if it contains a space).
...
Try:
SELECT
JSON_EXTRACT(
`saved_stuff`,
'$."34f8d790-6e25-4f31-ae8a-a17c04c96045"'
) IS NOT NULL
FROM
`table`;
See dbfiddle.
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
Whenever I tried to save my table using double data type this error appears please help I'm a newbie.
So your answer depends upon how you want to add double value in MYSQL.
If you want to query MYSQL through SQL then you should do the query using decimal value in your query instead of a comma.
a query should be like this...
INSERT INTO `comment` ( `no_of_comments` , `newField` ) VALUES (12,23.5)
Here newField is the double type and this query works fine.
And if you want to insert the default Decimal value in MYSQL database using PHPMYADMIN portal then you should add double value using the comma for decimal as...
Don't forget that you add comma just for defining the length of the decimal part, You add value in a proper double format for defining the value of the field.
There might have issue in Table Structure.
You have to Set Double Datatype with Length,Values Format.
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;