Getting the Primary key of the row which can not be parsed - json

I am trying to parse a json value from a column in Azure Sql Database with the following query
select Key, JSON_VALUE(JsonField, '$.Total')
from MyTable
However I am encountering some error in parsing that produces the following message
[12:37:32] Started executing query at Line 12
Msg 13609, Level 16, State 2, Line 1
JSON text is not properly formatted. Unexpected character '.' is found at position 0.
Is there any way to understand which rows have the following problem by, for example, having the column set to NULL by default in the return?
This way I can make a direct check on the resulting field.

You can use the T-SQL ISJSON function to identify problem values:
SELECT
Key
, JsonField
FROM dbo.MyTable
WHERE ISJSON(JsonField) = 0;

Related

Storing non-json data into a column with type not NVARCHAR(MAX), which is used for a JSON property index, throws an error

I've a problem storing non-json data into a column which is used for a JSON property index. After the index has been created, storing non-json data into the column results in the following error: [S0001][13609] Line 1: JSON text is not properly formatted. Unexpected character 'n' is found at position 0.
I've created the computed column needed for the index and the index itself like this:
ALTER TABLE foo ADD vBar AS IIF(ISJSON(DETAILS_JSON) > 0, JSON_VALUE(DETAILS_JSON, '$.bar'), NULL)
CREATE INDEX IDX_FOO_BAR_ID ON foo (vBar)
When trying to store non-json data in the column (e.g. UPDATE foo SET bar = 'simple text') it results in the error mentioned above.
However, when I execute the example and store non-json data in the column it works...
The problem was the length of the column where the JSON data is stored. The length has to be MAX - if it is different (e.g. NVARCHAR(4000) or VARCHAR(4000)) it results in the mentioned error.
I. e. the type of the column has to be NVARCHAR(MAX) or VARCHAR(MAX).

Parse JSON into PL SQL with poor formatting

I am trying to parse JSON with poorly formatted code into sql table, however some of the values are creating problem.
Database is oracle 19.2
The json data is :
Insert into r_data (id,data)values
(1,'{'"View":"100",
"Assignment Title":"Collect all snippets from the Library",
"Status":"In Progress",
"Active/Not Active":"Depends"}');
I want the result as:
View Assignment_Title Status Active_Not_Active
100 Collect all snippets from Library In progress Depends
When i write the query,
select * from r_data x JSON_TABLE(x.data,'$',
COLUMNS (view NUMBER(10) PATH '$.view')
--for the first column view , it throws error of invalid identifier at the word view.
It only works fine for column Status as probably its single word and because view is keyword it is throwing problem. However i cannot change these names and want column View as View, Assignment Title as Assignment Title and Active/not Active as Active_not_Active.
How can this be done?
The main issue is that view is a reserved keyword, and therefore it can't be used as a column name (unless in double quotes - which is best avoided). You wanted to create a column named view from the data in your JSON - that's why you got the "invalid identifier" error.
But there are numerous other errors in your code; if you even made it as far as the "invalid identifier" error, that means that the code you posted is not the code you ran.
For example, even in the insert statement, you have an extra single-quote after the opening brace. That means that the opening brace is a string (one character) and the rest is who-knows-what. I had to remove that errant single-quote to make the insert work. How were you able to use that obviously syntactically incorrect statement?
In the select statement you are missing a comma after the alias x (before the keyword JSON_TABLE). No way you would get the "invalid identifier" error with that syntax error in the code.
In the JSON_TABLE function, there should be no comma between '$' and the COLUMNS clause. With that comma there, you would get a different error, not "invalid identifier" - so I don't believe that what you posted is your real code.
Etc. If all you got was an "invalid identifier" error, just choose a different name for the column (view won't work) and see what happens. Although... there is one more mistake, and it will result in an unexpected result. JSON is case sensitive. The attribute name in the JSON is View, with a capital V. So you must reference it as $.View in JSON_TABLE; you have $.view, which doesn't correspond to any attribute in your JSON, so you will get null for that column (if you don't change it to match capitalization from the JSON).
Here is the complete example, with all the errors corrected.
First, create the table. I do it all in one step:
create table r_data (id,data) as select 1,'{"View":"100",
"Assignment Title":"Collect all snippets from the Library",
"Status":"In Progress",
"Active/Not Active":"Depends"}' from dual;
Then, here is the query and its output. Notice the double-quotes around property names with embedded spaces (and forward slash, etc.).
select x.id,
j.view_, assignment_title, j.status, j.active_not_active
from r_data x,
json_table(x.data,'$'
columns (view_ number(10) path '$.View',
assignment_title varchar2(50) path '$."Assignment Title"',
status varchar2(20) path '$.Status',
active_not_active varchar2(20) path '$."Active/Not Active"'
)
) j
;
ID VIEW_ ASSIGNMENT_TITLE STATUS ACTIVE_NOT_ACTIVE
-- ----- ------------------------------------- ----------- ------------------
1 100 Collect all snippets from the Library In Progress Depends

Mysql JSON EXTRACT don't work with large key

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.

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