Values from xml data field in mysql - mysql

i would like to know if there is a query to select values from all of my xml data fields. There are around 1k rows which has xml data. All of them has almost the same data structure. With extract value i was able to extract one data field but at the point where more than one row is part of my subquery it breaks.
Here is an example xml data inside my db:
<EDLXML version="1.0.0" type="variable">
<properties id="template_variables">
<deliveredDuration>4444</deliveredDuration>
<deliveredNum>1</deliveredNum>
<comment/>
<projectname>cdfkusen</projectname>
<name>kral_schalke_trenink</name>
<order_id>372846</order_id>
<cutlistId>2763_ID</cutlistId>
<bcutlistId>51ddgf7a6-1268-1gdfged-95e6-5254000e8e1a</bcutlistId>
<num>1</num>
<duration>177760</duration>
<quotaRelevantDuration>0</quotaRelevantDuration>
<organisationUid>OrgName</organisationUid>
<organisationQuota>333221233</organisationQuota>
<organisationUsedQuota>123</organisationUsedQuota>
<organisationContingentIrrelevantQuotaUsed>54</organisationContingentIrrelevantQuotaUsed>
<userDbId>7xxxx84-eb9b-11fdsb-9ddd1-52cccccde1a</userDbId>
<userId>xxxx</userId>
<userRights>RH_DBC</userRights>
<firstName>DThom</firstName>
<lastName>Test</lastName>
<userMail>xxx#ccc.cz</userMail>
<language>English</language>
<orderTimestamp>1659448080</orderTimestamp>
<stitching>false</stitching>
<transcode>NO</transcode>
<destination>Standard</destination>
<collaboration>private</collaboration>
<premiumUser>false</premiumUser>
<priority>normal</priority>
<userMail2>xxx#ccc.cz</userMail2>
<cutlistItems>
<cutListId>125124_KFC</cutListId>
<cutListItemId cutlistItemDeliveryStatus="&#10004" cutlistItemDStatusMessage="delivered">112799</cutListItemId>
<bmarkerId>8f16ff80-1269-11ed-95e6-5254000e8e1a</bmarkerId>
<videoId>2912799</videoId>
<counter>1</counter>
<frameInSpecified>true</frameInSpecified>
<frameIn>15638</frameIn>
<frameOutSpecified>true</frameOutSpecified>
<frameOut>20082</frameOut>
<tcIn>00:10:25:13</tcIn>
<tcOut>00:13:23:07</tcOut>
<duration>177760</duration>
<BroadcastDate>2021-07-24</BroadcastDate>
<eventDate>2021-07-24</eventDate>
<resolutionFacet>HD</resolutionFacet>
<provider>DBC</provider>
<technicalrightholders>RH_DBC</technicalrightholders>
<rights>DBC</rights>
<materialType>DP</materialType>
<targetFilename>kral_schalke_trenink</targetFilename>
</cutlistItems>
</properties>
</EDLXML>
I got the right value from query if i do:
SELECT ExtractValue((SELECT job_xml from cutlist where job_xml is not null LIMIT 1), '//deliveredNum');
But when i change the limit amount i get back: Subquery return more than one row.

extractvalue expects two string arguments. When your subquery returns more than one row, you are not simply passing a string as the first argument (you are passing a set of results).
Instead of calling extractvalue once for your entire query, call it once for every row, like:
SELECT ExtractValue(job_xml, '//deliveredNum')
FROM cutlist
WHERE job_xml IS NOT NULL

Related

how to handle nested json data in mysql to insert multiple values in a single column

{"rows": [{"index": 1,"values": [{"variable": "var17ffec5e747","index": 1,"value": "Annexure -1A"},{"variable": "var17ffed3e1d5","index": 2,"value": {"fieldValue": "1","displayValue": "OSAA LIQUID-AQ GRADE"}},{"variable": "var17ffec97dac","index": 3,"value": "401511201"},{"variable": "var17ffec9941d","index": 4,"value": "kg"}]}],"rows": [{"index": 1,"values": [{"variable": "var17ffec5e747","index": 1,"value": "Annexure -1A"},{"variable": "var17ffed3e1d5","index": 2,"value": {"fieldValue": "1","displayValue": "OSAA LIQUID-AQ GRADE"}},{"variable": "var17ffec97dac","index": 3,"value": "401511201"},{"variable": "var17ffec9941d","index": 4,"value": "kg"}]}],"rows": [{"index": 1,"values": [{"variable": "var17ffec5e747","index": 1,"value": "Annexure -1A"},{"variable": "var17ffed3e1d5","index": 2,"value": {"fieldValue": "1","displayValue": "OSAA LIQUID-AQ GRADE"}},{"variable": "var17ffec97dac","index": 3,"value": "401511201"},{"variable": "var17ffec9941d","index": 4,"value": "kg"}]}]}
` this is raw_json code and that will have multiple rows data, i tried to fetch only key value of "displayValue" in each rows and concat each rows "displayValue" with comma(,) seperated. but i am failed to do this
Can anyone give solution, how to solve this problem`

Pull data from JSON column and create new output with ABSENT ON NULL option

I have a JSON column in an Oracle DB where it was populated without the ABSENT ON NULL option and there are some pretty long lengths because of this.
I would like to trim things down and have created a new table similar to the first but I would like to select the JSON from form the old, add the ABSENT ON NULL option and place the new values in reducing the column length.
So I can see the JSON easy enough like
SELECT json_query(json_data,'$') FROM table;
This will give a result like:
{
"REC_TYPE_IND":"1",
"ID":"1234",
"OTHER_ID":"4321",
"LOCATION":null,
"EFF_BEG_DT":"19970101",
"EFF_END_DT":"99991231",
"NAME":"Joe",
"CITY":null
}
When I try to remove the null values like
SELECT json_object (json_query(json_data,'$') ABSENT ON NULL
RETURNING VARCHAR2(4000)
) AS col1 FROM table;
I get the following:
ORA-02000: missing VALUE keyword
I assume this is because the funcion json_object is expecting the format:
json_object ('REC_TYPE_IND' VALUE '1',
'ID' VALUE '1234')
Is there a way around this, to turn the JSON back into values that JSON_OBJECT can recognize like above or is there a function I am missing?

How to search a json encoded array column in a table with respect to an array and return the matching result?

I have a mysql table that contains a column user_ids - this column holds json encoded array of user ids.
Just like this -
row0 - "[145,146]",
row1 - "[145]",
row2 - "[141,143,145]",
row3 - "[142,146,147]"
Now I want to search inside the user_ids column for each row with respect to another array of user_ids supplied as an argument. I want all the matched rows to be returned as a result of this query.
I have tried to implement it like this in Laravel but it didn't return any rows-
$booked_courses = $booked_courses->where(function ($query) use ($user_ids) {
$query->orWhere('user_ids', 'LIKE', '%'.json_encode($user_ids).'%');
});
Also I thought looping over the decoded array might yield the result but didn't succeed, tried to do the following -
$booked_courses = $booked_courses->where(function ($query) use ($user_ids) {
foreach(json_decode($query->user_ids) as $u_id) {
$query->whereIn($u_id, $user_ids);
}
});
Any help in raw sql as well, will be very helpful for me. Or please suggests me some ways to fetch the desired result. Thank you so much for your time.

How to retrieve a data from inside a json postgres

In my database table I have a column named data and that column's type is jsonb. Here is a sample json of the column.
{"query": {"end-date": "2016-01-31", "start-date": "2016-01-01", "max-results": 1000, "start-index": 1 }}
This is the result in a formal format.
{
"query":{
"end-date":"2016-01-31",
"start-date":"2016-01-01",
"max-results":1000,
"start-index":1
}
}
I need to get the data from the 'start date' inside the 'query' element. How get the data from the start date from a pgsql query
You can use the Postgres in-built function named 'json_extract_path'. Document.
The first parameter in this function is the column name, the second parameter is JSON root element and the third parameter is the key name of which you want to get data.
select json_extract_path(data::json,'query','start-date') as test FROM "schema".tbl_name

MariaDB COLUMN_JSON query returns binary

I've been trying to use dynamic columns with an instance of MariaDB v10.1.12.
First, I send the following query:
INSERT INTO savedDisplays (user, name, body, dataSource, params) VALUES ('Marty', 'Hey', 'Hoy', 'temp', COLUMN_CREATE('type', 'tab', 'col0', 'champions', 'col1', 'averageResults'));
Where params' type was defined as a blob, just like the documentation suggests.
The query is accepted, the table updated. If I COLUMN_CHECK the results, it tells me it's fine.
But when I try to select:
"SELECT COLUMN_JSON(params) AS params FROM savedDisplays;
I get a {type: "Buffer", data: Array} containing binary returned to me, instead of the {"type":"tab", "col0":"champions", "col1":"averageResults"} I expect.
EDIT: I can use COLUMN_GET just fine, but I need every column inside the params field, and I need to check the type property first to know what kind of and how many columns there are in the JSON / params field. I could probably make it work still, but that would require multiple queries, as opposed to only one.
Any ideas?
Try:
SELECT CONVERT(COLUMN_JSON(params) USING utf8) AS params FROM savedDisplays
In MariaDB 10 this works at every table:
SELECT CONVERT(COLUMN_JSON(COLUMN_CREATE('t', text, 'v', value)) USING utf8)
as json FROM test WHERE 1 AND value LIKE '%12345%' LIMIT 10;
output in node.js
[ TextRow { json: '{"t":"test text","v":"0.5339044212345805"}' } ]