Error Code: 3143. Invalid JSON path expression. The error is around character position 3 [duplicate] - mysql

How can I access values in the status and date columns stored as JSON?
Please, have a look at an example row below.
{"1":{"status":true,"date":"2022-03-30"},"3":{"status":true,"date":"2022-03-30"}}

Demo:
set #j = '{"1":{"status":true,"date":"2022-03-30"},"3":{"status":true,"date":"2022-03-30"}}';
select json_extract(#j, '$."1".status') as status;
+--------+
| status |
+--------+
| true |
+--------+
In this case, it may be unexpected that you need to put double-quotes around "1" to use it in a JSON path.

Related

How to convert a string value of json into dateTime and compare

SELECT JSON_EXTRACT(z2schedule,'$[*].start') as startDate from
cpmdev_z2weekly_schedule
After running above code I am getting response as :-
Now If I tried to compare each value to time value using below code but it is not working:-
SELECT JSON_EXTRACT(z2schedule,'$[*].start') as startDate from
cpmdev_z2weekly_schedule where
JSON_EXTRACT(z2schedule,CONVERT('$[*].start'),'TIME')>
'CONVERT('2022-11-02 13:10:00:000', TIME)
My requirement is only to compare each value with the time value and return only if the value is greater than given time.
For Example in Table I have Data as:-
[{"start":"09:00:00.000","end":"17:00:00.000"}]
[{"start":"10:00:00.000","end":"17:00:00.000"}]
[{"start":"11:00:00.000","end":"17:00:00.000"}]
Now I want all the start Date which is greater then 10:00:00
In above case then it should return :
11:00:00.000
The JSON you show is an array of objects. When you use $[*].start, it returns a JSON array. This is not a single time. You can see the square brackets around the time value:
mysql> set #j = '[{"start":"09:00:00.000","end":"17:00:00.000"}]';
mysql> select json_extract(#j, '$[*].start') as times;
+------------------+
| times |
+------------------+
| ["09:00:00.000"] |
+------------------+
The square brackets make it not valid as a time value.
mysql> select convert(json_extract(#j, '$[*].start'), time) as times;
+-------+
| times |
+-------+
| NULL |
+-------+
Since your JSON array seems to have only one object in it, you could use $[0] to select the first object in the array. Then it returns a single string value and that is convertable to a time:
mysql> select convert(json_extract(#j, '$[0].start'), time) as time;
+----------+
| time |
+----------+
| 09:00:00 |
+----------+
Note also that the data type named in the CONVERT() function is a keyword, not a quoted string. That is, 'time' is incorrect, just use time.
If your JSON array may have more than one object, and you need to test all of them, then you should use the JSON_TABLE() function.
By the way, all these issues would be avoided if you stored your start and end times in normal rows and columns. Using JSON makes many queries more difficult to develop and optimize. You should consider normalizing your data, and not using JSON.

Access JSON column

How can I access values in the status and date columns stored as JSON?
Please, have a look at an example row below.
{"1":{"status":true,"date":"2022-03-30"},"3":{"status":true,"date":"2022-03-30"}}
Demo:
set #j = '{"1":{"status":true,"date":"2022-03-30"},"3":{"status":true,"date":"2022-03-30"}}';
select json_extract(#j, '$."1".status') as status;
+--------+
| status |
+--------+
| true |
+--------+
In this case, it may be unexpected that you need to put double-quotes around "1" to use it in a JSON path.

whats wrong in below query Select CONVERT(xml,'<x>' + Replace(A.name,':','</x><x>')+'</x>' ) as xDim from Erecharge;

I want to convert string to xml column ..
I used below query for that :
Select CONVERT(xml,'<x>' + Replace(A.name,':','</x><x>')+'</x>' ) as xDim from Erecharge;
but it shows error of incorrect sql syntax..
I want to know whats wrong in above query
I also tried this:
Select Cast('<x>' + Replace(A.name,':','</x><x>')+'</x>' as XML) as xDim from Erecharge;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'XML) as xDim from Erecharge'
This means that XML is incorrect in a expression like this:
CAST('foo' AS XML)
As per the docs, the values allowed for CAST type do not include XML.
Additionally, using the + operator on strings is just a convoluted way to render zero:
mysql> SELECT 'a' + 'b';
+-----------+
| 'a' + 'b' |
+-----------+
| 0 |
+-----------+
1 row in set, 2 warnings (0.00 sec)
It's not entirely clear what you're trying to do. MySQL has XML Functions but it doesn't have XML data types. If you just want to produce a string that happens to contain XML code then you need to CONCAT():
mysql> SELECT CONCAT('<date>', CURRENT_TIMESTAMP, '</date>') AS foo;
+----------------------------------+
| foo |
+----------------------------------+
| <date>2018-10-12 11:44:29</date> |
+----------------------------------+
1 row in set (0.00 sec)
... but of course you still need to ensure that angle brackets and similar stuff don't break the XML. CDATA may help. (No idea about XML functions, I'm not familiar with them.)

How can I convert the string values inside a MySQL JSON array to upper case?

I have a table that contains a JSON column, and in it a JSON array:
mysql> SELECT profile->'$.countriesVisited' from users;
+-------------------------------+
| profile->'$.countriesVisited' |
+-------------------------------+
| ["us", "il"] |
| ["co", "ph"] |
+-------------------------------+
2 rows in set (0.00 sec)
I want to convert the values inside the array into upper case. (I am assuming this answer would also assist lower case, string replacements.. etc.)
I've been trying to use UPPER, JSON_ARRAY, JSON_QUOTE, JSON_UNQUOTE, etc - at best I end up with a string representation of what I want.
How can I do this? I'm running MySQL 5.7.19.
You need to use JSON casting. Try the following:
UPDATE users
SET profile = JSON_SET(
profile,
'$.countriesVisited',
CAST(
UPPER(profile->'$.countriesVisited')
AS JSON
)
);

MySQL JSON_EXTRACT path expression error

The syntax looks right to me, any help would be appreciated!
mysql> select fieldnames from tablename limit 5;
+--------------------------------------------------------+
| fieldnames |
+--------------------------------------------------------+
| {"example-field-1": "val2"} |
| {"example-field-2": "val1"} |
| {"example-field-1": "val1", "example-field-3": "val1"} |
| {"example-field-2": "val1"} |
| {"example-field-2": "val2"} |
+--------------------------------------------------------+
mysql> select JSON_EXTRACT(fieldnames, '$.example-field-1') from tablename;
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 17 in '$.example-field-1'.
MySQL 5.7.10
Can you try this advice from https://dev.mysql.com/doc/refman/5.7/en/json.html
As mentioned previously, path components that name keys must be quoted
if the unquoted key name is not legal in path expressions. Let $ refer
to this value.
select JSON_EXTRACT(fieldnames, '$."example-field-1"') from tablename;
NOTE
If you have more fields, you must quote each key not the whole path:
select JSON_EXTRACT(fieldnames, '$."field"."example-field-1"') from tablename;