Parse JSON into PL SQL with poor formatting - json

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

Related

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.

Syntax error in date in query expression for non-date fields

I'm having trouble building a query in Access 2013. The database isn't mine and the only thing I really have control over is this query. There is a table, I'm pulling 7 fields from it and eventually adding an 8th field to the query to do some string manipulation.
However, I keep getting getting "Syntax error in date in query expression 'fieldname'." error whenever I click on the arrow to sort the fields. The odd thing is these errors pop up when sorting non-date fields. When sorting the date field I get "Syntax error (missing operator) in query expression 'Release Date'."
This happens after a fresh build. I have no WHERE conditions, just SELECT and FROM. Ideas?
Here's the sql query, though I'm mainly working in the query design view:
SELECT Transmissions.[Job#], Transmissions.[Part#], Transmissions.TransmissionSN, Transmissions.Status, Transmissions.[Release Date], Transmissions.[Build Book Printed], Transmissions.[ID Tags Required]
FROM Transmissions;
Well... it seems you are the lucky inheritor of a poorly designed database.
Using special characters in a field name is just asking for trouble. And you've found what that trouble is.
Access uses the # sign to designate a Date type for query comparisons. Such as:
dtSomeDate = #2/20/2017#
You surround the date with the # signs.
In your case, the query thinks [Job#] and [Part#] are trying to wrap dates. But of course, that's not the case and thus it fails.
You can try a couple of work arounds. (I leave it to you to experiment.)
1) You can try to rename the problem fields within your query. So that:
Transmissions.[Job#] becomes Transmissions.[Job#] as JobNum
and
Transmissions.[Part#] becomes Transmissions.[Part#] as PartNum
2) You can try to copy the [Transmissions] table to a new table that you create
that does not have the naming problems.
3) Export the [Transmissions] table to a CSV file and re-import it to a new
table (or possibly new database) without the naming problems.
Here is a link to a microsoft article that tells you why to avoid special characters in Access:
Big Bad Special Chars.
Hope that puts you on the right track. :)
Typically, this means that the field names are missing or misspelled.
Try running this to see:
SELECT * FROM Transmissions;

SQL fulltext-search with CONTAINS raises geometry error

My php code generate a sql query as follow:
SELECT id, title, files, rating, DATE_FORMAT(date, '%d %M %Y') AS fancydate
FROM ArtArchive
WHERE CONTAINS (tags, 'Pepper')
ORDER BY date DESC
And it returns the error:
HY000 - 3055 - Geometry byte string must be little endian.
tags is a VARCHAR(255) indexed as FULLTEXT
I got the query to work by using : tags LIKE '%Pepper%', so I'm certain the CONTAINS is in cause.
I couldn't figure out what this error mean, nor what I have to do with geometry of all things: I'm simply trying to find words in a text.
After checking multiple examples, I'm pretty sure that I've been using the correct syntax. I also tried things such as : CONTAINS (tags, '"Pepper"'), CONTAINS (tags, 'Pepper'), CONTAINS (tags, "'Pepper'"), with no different result.
I'm trying not to use IN or LIKE because I intent to search multiple values at once in the tags field. I've toned it down to one value in the example until I get get it to work at all.
MySQL does NOT support CONTAINS() for text searches, as you would expect:
MySQL only recognizes the CONTAINS SQL function when dealing with
spatial data. It requires two graphics objects as arguments, and
returns a 1 or 0 depending on if the first object completely contains
the second. Designed as an implementation of the OpenGIS framework,
the MySQL CONTAINS function does not work on ordinary strings, and
will produce an error if you try it. MySQL only recognizes the LIKE
and STRCMP functions when dealing with string of information.
Found that info here.

mysql 0 terminated string showing up in XML as Unicode 0x0

In the process of moving some applications from PHP and Delphi to Java-RS some column entries are spoiling the show at this time.
After reading the data from mySQL with JPA into Java Pojos converting the result to XML using JaxB and trying to read back the result to JQuery / jqGrid a failure happens.
In the browser the problem simply shows at "not well formed".
Looking at the details with the Eclipse XML editor gives the error message:
An invalid XML character (Unicode 0x0) was found in the element content of the document
Now I'd like to proceeed and fix the original data.
How would an SQL query look like that looks for the rows that have
invalid entries?
How would an SQL query look like that fixes these
rows?
Let's assume the column with the problem is "name" in Table "Customer"
For question #1 I found:
SELECT name from customer where hex(name) like "%00";
to work. For question #2 I am assuming that update with a left substring might work. I am not sure about the length in this case. It looks like length(name) will return the length including the terminating zero character. Will the update with left(length(name)-1) work correctly in this case?
I am not sure whether backup and restore of the database would keep the current somewhat corrupted database in shape. So it would also be helpful to know how the situation can be reproduced with an insert statement that creates null terminated strings on purpose.
I think you should be able to transform the HEX() with something like
UPDATE customer SET name=UNHEX(REPLACE(HEX(name), '00', ''));
or simply
UPDATE customer SET name=REPLACE(name, CHAR(0), '');
update customer set name=left(name,length(name)-1) where hex(name) like "%00";

Cannot Update a Numeric Named Column

I am trying to perform a simple task in SQL Server and receiving an error. I have a table called World.Internet_CountryYear. The table has year columns (i.e. 1960, 1965, etc.) with numerical data in the fields, but the data type for the columns is varchar(max). Currently, the fields that do not have data have "" in them, which I would like to replace with 0.00. So, I try to run a simple update command as follows:
UPDATE World.Internet_CountryYear
SET 1960 = '0.00'
WHERE 1960 = '""'
For some reason, I receive an error saying "Incorrect syntax near '1960'"
Is there a problem with the DDL of this table? What am I missing here? I have tried specifying World.Internet_CountryYear.1960 as well, but still receive a similar error: Incorrect syntax near '.1960'
Any suggestions?
Enclose the column name with square brackets (you need to use the square brackets when you are using skeywords, special characters in the column names)
Try this:
UPDATE World.Internet_CountryYear
SET [1960] = '0.00'
WHERE [1960] = '""'